Showing posts with label Coding. Show all posts
Showing posts with label Coding. Show all posts

Friday, 17 February 2017

Quick tip: moving an Eclipse project to another folder

I am restructuring some projects on my local machine. Usually I don't want to touch a running project but since I have the opportunity to use an internal Bitbucket server now at my workplace I also want to have all my local project source files on one location.
So this is how I moved the project source files to another location on my local machine:
  1. Open Eclipse and go to the Package Explorer view
  2. Check your project folder locations

    So the project is located in the folder D:\sample\net.mruhnau.sample.hello
  3. Right-click on the projec in the Package Explorer and choose Refactor --> Move...
  4. In the following dialog, choose the new folder location for the project and click OK.
This is it, Eclipse moves the files to the specified location. The advantage is that using this procedure will retain your workingsets as well as project information, history etc. I think this is a very smooth way of reorganizing project files.

Thursday, 3 December 2015

Image processing using JaxaXT in a Notes / Domino Agent



Today I stumbled across JavaXT. JavaXT is described as
a collection of Java libraries and utilities that provide a number of functions not available in the standard JDK. It is an extension of the Java core, written to simplify Java development.

I needed a library to resize images - from a good old java agent :-/  So far I used the Java Advanced Imaging API  but I had issues resizing some JPEG images. I was not able to find a correlation what caused the issues: And to be honest, my code was not very clean and neat so this was a good opportunity for improvements anyway. 
Using JavaXT I refactored my whole code to very few lines to do some resizing of images and save them in a NotesDatabase. 

This sample processes all Files in a given folder and resizes them to a fixed width of 200 pixel. 

import java.io.File;
import lotus.domino.*;

public class JavaAgent extends AgentBase {

  public void NotesMain() {

    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      File[] files = new File("D:/tmp/pix/").listFiles();
      for (File file : files) {
        if (!file.isDirectory()) {
          String srcFileName = "D:/tmp/pix/" + file.getName();

          javaxt.io.Image image = new javaxt.io.Image(srcFileName);

          String destFileName = "D:/tmp/pix/resize/" + file.getName();
          image.setWidth(200);
          image.saveAs(destFileName);
          
        }
      }

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Job done! In a few lines of code :-)
Besides image processing JavaXT provides a lot more, I can only recommend to check it out. 
If you have suggestions , e. g. for a better approach  I am more than happy about your feedback.

Tuesday, 17 March 2015

Apply Java Permissions to Domino Agents at runtime - again ;-)

This post is supposed to be read by developers only. Admins, please move on ;-)

I have an agent which is used to make SFTP file transfers from Domino to our corporate SAP system. It's an interesting project and I am exploring new stuff on a daily basis :-)

One technical issue I had to solve was this:
The agent opens an SFTP session on a remote host. Unfortunately this is forbidden by Java Policies specified in the java.policy file of the JVM of the Domino server.
The error is
java.lang.SecurityException: not allowed to make a socket connection to MYHOST.MYDOMAIN.COM,-1

So I started to dig into java.policy and java.pol files. And to be honest I was not that much pleased about the whole thing. At first I started to grant ANY permissions in the global section of my policy file:


 grant {   
  ...  
  permission java.security.AllPermission;  
  ...  
 };   

This works but it goes a bit over the top for a productive environment... Therefore I started to tweak the policies to only allow one specific application the network operations required to do the job. Honestly, it's been painful... I think I got it more or less configured properly. But given the fact that we run the application in three different environments (DEV/TEST/PROD) on multiple servers I hardly could imagine a proper rollout scenario. I always thought one of the major strengths of domino and NSF was that usually the design and resides in a single database - and the configuration usually resides in the application, a few system databases (+ notes.ini).

There must be an easier way to grant my code the execution rights it needs.
I finally found a solution to grant my code the needed execution rights at runtime of the agent. There is a whole Java package for handling the permissions - and in my particular case I was looking for SocketPermissions. The code to grant a socket permission is pretty straight forward:

 String hostAndPort = "myhost.mydomain.com:22";   
 SocketPermission p = new SocketPermission(hostAndPort,  
       "accept,connect,resolve");  


This will allow my code to make a network connection to the specified host on the specified port. There might be neater solutions to that problem and not every admin would be happy about that. In my opinion it is not very consistent for what you need to grant permissions in policy files (like network connects) but on the other hand file handling can be allowed on the agent security (Allow restricted operations).


Thursday, 8 May 2014

Extract Top-Level-Domain from a URL using client-side JavaScript


I needed a way to extract the top-level-domain from a url in a client-side javascript. I use the window.location object to achieve this (as I needed it in a CSJS anyway). There might be an easier way to do this, but this is my version of doing this:

 /*  
  * This function returns the TLD part of the url based on window.location => CSJS   
  */  
 function getTLD() {  
      var hostName = window.location.hostname;  
      var hostNameArray = hostName.split(".");  
      var posOfTld = hostNameArray.length - 1;  
      var tld = hostNameArray[posOfTld];  
      return tld;  
 }  

Looking forward to your approacches to to this ;-)

Thursday, 16 January 2014

Some thoughts on Source Control Management for XPages development

In my career as a Domino developer I have been playing with various Source Control Managent solutions already. I used TeamStudio Ciao! for many many years and this tool proved to be a life saver in many occasions already. So I owe some drinks to the guys from TeamStudio... (don't tell them!!)

Since IBM started opening Domino Designer in Release 8.5.3 (was it 8.5.2 ???) towards the "standard" SCM systems (Git, SVN, Mercurial) I have been playing with these options a lot. There was quite some pain involved, I lost source code, my repositories lost the link to the NSF/ODP, etc... No pain, no gain! The good news for me: Today I have a working environment which fits my needs :-)
My first steps were with SVN, but I admit that I was not able to get this setup running properly. Additionally, I was not too happy about the centralized approach. That said I started using Mercurial. Finally I ended up using Git, first the implementation in Domino Designer and nowadays using SourceTree. This setup provides me with such a high flexibility with regards to my work-related projects as well with my pet-projects.

The last missing bit so far was a proper SCM system. I have been looking around for a while and playing with various systems. As I am a corporate developer, using a hosted environment was not an option at all. So I was looking for a solution to run my self-hosted SCM server for my code coming from both, work-related projects as well as pet-projects. Finally, I think my journey has ended successfully (or at least I have reached an intermediate stop). I have played with GitLabHQ for a a while now and it looks very promising to me. I got it up and running relatively painless on a local Ubuntu VM. If you are looking for a self-hosted SCM server, I can highly recommend looking into GitLab (it is very focussed on SCM though).
There is a lot of useful information available in- and outside the XPages community. In the upcoming weeks I will describe my environment and approaches and specifically GitLab a little more in detail and hopefully add some more useful bits. So stay tuned!

Tuesday, 19 November 2013

DOTS: Logging to server console and log.nsf (part2)

In my previous blog post I showed how I implemented console logging in my DOTS projects. As I mentioned already yesterday, I prefer using logMessage respectively logException which are both implemented in the ServerConsole class of DOTS.
Both methods can easily be used directly inside your tasklet. When my first tasklet was growing, I started implmenting my own classes. Thereby I realised that logMessage and logException both are not available outside the tasklet class.

As these methods are implemented inside an own class "ServerConsole" I need an object of this class to use these methods. First I declare an object of type ServerConsole. In my case this object is in my class header and initialized in the class constructor. Thus I can use this object anywhere in my class - and even outside my class as it is declared as a public object.


In my DOTS projects I usually declare one ServerConsole object in one of my classes and use this object anywhere possible.

Beyond this approach I was pointed into two different directions today:

Rene Winkelmeyer mentioned another useful approach in a comment  here. If you want to log outside of the tasklet class you can easily use ServerTaskManager.getInstance().logMessageText("").


So if you only want to log messages, this might be the easiest solution for you. However, if you want to log messages and/or exceptions, the ServerConsole class is your friend...
OR you go down the road that Serdar suggested in his comment here. and use his Logger class.
He created an own TaskletLogger class which extends another Logger class. I haven't tried it out yet, but I am pretty sure it works great :)
Speaking of logger classes, I think I have to mention the XPages OpenLog Logger OpenNTF project by Paul Withers which can be used from OSGi extensions too.

To make a long story short: there are many options available - and while writing this update post I am realizing that this "blogging thing" revealed a lot of good new stuff to explore ;-)

Monday, 18 November 2013

DOTS: Logging to server console and log.nsf

In one of my current development projects I am developing a DOTS server task(let). One thing that I found very annoying is the logging part of DOTS. With logging, I essentially mean - at least in this post - an output to the server console and / or log.nsf. To achieve this, there are basically two different possibilities for logging in your DOTS projects.


Option 1:  System.out.println
As in other Java projects you can use System.out.println("your message") to print out message to the server console.
Running this little tasklet results in the following output on the server's console:

As you can see, the message is printed to the console. What you cannot see from here is that these messages are not save to log.nsf.

Option 2: logMessage
A way better logging is provided by a specific method logMessage("your message") which is part of the class com.ibm.dots.task.ServerConsole.
While your cord  resides inside the tasklet you can easily use this method instantly without any further declarations:
The output looks like that:


Using this method has several benefits:
  • Messages logged using the logMessage method are saved to log.nsf. 
  • You can see the date and time of the message.
  • In addition, the message indicates that it comes from the DOTS Tasklet "Helloworld".
In my opinion this is the clean way to go for logging from a DOTS tasklet to the console respectively log.nsf.

Moreover, the ServerConsole class provides another very useful method "logException" which accepts a throwable Java Exception which then will be printed to the console. In other words: you can use this method in any catch statemen to handle error logging to the server console / log.nsf.



Friday, 15 November 2013

XPages iNotes Calendar control - Events oddities

While working in one of our projects with the iNotes calendar control I experienced some oddities with the Events of this control. What I wanted to achieve is that some magic happens when clicking somewhere inside the control - e. g. opening the entry on which the user clicks... Should be pretty straightforward, I was thinking. Stupid me! As usual the devil's in the details.

I started by selecting the control and opening the events view and entered a simle JavaScript in the "onNewEntry" event. The script is supposed to show a message box after  clicking in an empty time slot.
 
In my case the result is that the event just doesn't get fired at all. I tested with FireFox, Google Chrome and even Internet Explorer. After some more testing and playing with the control I noticed an odd behaviour of Domino Designer:
As you can see on the Screenshot above, both events "onNewEntry" and "onOpenEntry" are marked blue which indicates, that there is some code in the event to be executed.  When I opened the "All Properties" view of the control to check the scripts there, I noticed that both events were empty, no script, nada, nil... Switching back to the events view, the scripts were there.





So that got me thinking. Once I entered the same code in an event from the "All Properties" view, the code is executed.

As far as I understand, the "Events" view is kind of buggy for at least this control. Besides the issue described above, I noticed that from the "Events" view I can enter CSJS as well as SSJS and Simple Actions. From the "All Properties" view, only CSJS can be used - and as far as I can see only CSJS is supported. Side note: I am using Domino Designer and Domino Server R 9.0.

 Lesson learned (again): All Properties is where you should do your work!The good news: as soon as you know how to work with this control, it works great :-)