Category Archives: developing

mockito

mockito is a pretty cool mocking framework. It is simple and logic… actually i am using it with interfaces, which makes it really logic and simple, dont know, who it works against normal classes… will test that soon 🙂

but first, how does it work with a simple calculator class:

HelloWorld helloWorld = Mockito.mock(HelloWorld.class);
Mockito.when(helloWorld.hello(Mockito.anyString())).thenReturn(“Hello”);

  • So first line initializes the mock.
  • Second line defines the return value of the function hello(String) when called with any string. So it allways returns Hello.

foreach in java

for(String item : list){
       // do things with the item
}

accessing imap

accessing imap mailboxes is super easy with javax.mail… 

final Properties props = new Properties();
props.setProperty(“mail.store.protocol”, IMAP_PROTOCOL);
final Session session = Session.getInstance(props);

final Store store = session.getStore(IMAP_PROTOCOL);
store.connect(server, user, password);

final IMAPFolder imapFolder = (IMAPFolder) store.getFolder(“Inbox”);
imapFolder.open(Folder.READ_ONLY);

final FlagTerm ft = this.getFlagTerm(new Flags(Flags.Flag.SEEN), false);
final Message[] messages = imapFolder.search(ft);

for (int i = 0; i < messages.length; i++) {
  final long imapUid = imapFolder.getUID(messages[i]);
  final MimeMessage message = new MimeMessage((MimeMessage) messages[i]);
}

easy… main problem is, that the MimeMessage is directly linked to the imap Folder… so if you return the MimeMessages and close the imapFolder… accessing the MimeMessage dont work… 🙁

Solution? CopyConstructor

MimeMessage copyedMessage = new MimeMessage(message);

nuff said!

mule with spring-beans and autowire…

… doesnt work 🙁

i really think (now partly thought) spring is cool… i used a spring-bean configfile, initialised all the entities and @Autowired them.. pretty cool… reflection on the entities to find and load them… and i think its really readable in java code… whenever you see an @Autowired… it will be created someware completely different and reflected to the position…

but autowiring with mule dont work… or i couldnt figure out how… mule runs its own context and the springcontext is somewhere in there… and the springcontext behaves different… crap… so new solution:

beans.xml

<bean id=”A” class=”ch.michio.spring.A”/>

<bean id=”B” class=”ch.michio.spring.B”>
     <property name=”a” ref=”A”/>
</bean>

so in class B there was an:

@Autowired
private A a;

 now there is a setter:

private A a;
public setA(A a){
     this.a = a;
}

So during initialisation

  1. an A object is created
  2. a B object is created
  3. a in spring bean container found A is set to B

my main problem with this solution is the readability… you need to read the java files AND the beans.xml… bah… 🙁

mule collection-splitter

in our project a simple flow should read mails from an imap mailserver and process those… sounds simple, but isnt… the mails need to be left on the server for further use… and it is not guaranteed, that no other user might access the mailbox with an other client and do stuff… so no save SEEN, UNSEEN states… the imapUid is used to identify the mail…

ok simple task with mule… just take the imap:connector… wrong! the imap:connector is a cool connector to read unseen mails of an mailbox and returns a MimeMessage object, which does not contain the imapUid… so bla… unusable in our case… but the checkFrequency is a really important feature, because we dont know how many mails are in the inbox and a frontend-triggered start command is unusable, because of the delay… so we need an other solution: build it…

simple as well… wrong again!… ok not really wrong, but complicated… we use a quartz:inbound_endpoint to trigger the flow… a MailFetcher class is called, which gets all the needed information and… ehm… ok here is the problem… the quartz:inbound_endpoint sends one start command into the flow… the MailFetcher gets all the mails and sends them to the next workflow… but i really want single mails in the next flow and not a list of mails… so… how the hell can this be done?   FilteringListMessageSplitter is the solution… or lets say looked like…

  FilteringListMessageSplitter accepts a List as a message payload then routes list elements as messages over an endpoint where the endpoint’s filter accepts the payload. (link)

 actually it is the solution… but this class only has for services… and not for flows… for flows: <collection-splitter>

  The Collection Splitter acts on messages whose payload is a Collection type. It sends each member of the collection to the next message processor as separate messages. (link)

which really works…

And with an correct test environment it would be a peace of cake to figure it out… but returning null instead of a java.util.List might be a problem to split…

dependencies and repositories in maven

ok actually i didnt get the idea of dependencies and repositories in maven… it just worked… but know i do 🙂

instead of downloading and installing different jar files on your local maschine and on every other server, just tell maven to do it. with dipendencies maven knows which jars it should get and with repositories maven know where to get them… actually the mechanism stores an local copy of the jars in a local repository, and tries first to get it from there… and because of en unique jar version number, updating to new packages in the local repository is maven pom.xml driven.

with this mechanism it is really easy to guarantee the runability of an project… hmm… and when does maven install complete servers?

wsdl and maven

just created a contract-first wsdl… and actually its my first 😛 and yeah… there might be some errors… but those will be fixed at the end of the project… next year… 🙂

i created it in the frontend-clicky-thing of eclipse… kinda simple, but a bit random, how the wsdl will look like… lets just say, i hope for the best…

next task was to generate the java artifacts out of the wsdl file… with maven… and google really helped me: link

<plugin>
    <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>${cxf.version}</version>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>
is the plugin to generate the artifacts… but i m not so sure, that they really are and if they are really usable… but as i sead before… project end next year… enought of time to figure it out 🙂

apache cxf, tomcat and eclipse

apache cxf is my next stop… its a simple way to create webservices… and the tutorial is really hard… ok perhaps only for a for-a-long-time-no-java-developing developer, as i am… the main problem was, that the tutorial is based on your favorite ide… which in my case is eclipse, but without a little bit of help, my favorite ide is really confusing… but with a little help of other (unlinked) tutorials, a little bit of basic understanding of the (and some) concepts and a little bit luck i made it 🙂 ok not sure, but i see an autogenerated wsdl, which actually locks correct 🙂

so what did i do… setting up:

  1. downloaded apach cxf and added it to PATH (not sure if needed)
  2. in eclipse Windows–>Preferences–>Web Services–> CXF 2.x Preferences set the downloaded cxf folder
  3. intsalled a tomcat… its a lie, because it was installed before, but wth
  4. in eclipse Windows–>Preferences–>Server–>Runtime Environment added the tomact… lie again, was set already…

and now… project:

  1. New–>Dynamic Web Project (Name = CXFTest)
  2. Configured it with Target Runtime = Apache Tomcat and Configuration = CXF
  3. Then created all the files, based on the tutorial
  4. To Run: Right – clicked the web.xml and Run As–> Run on Server
  5. Access the WSDL: http://localhost:8080/CXFTest/HelloWorld?WSDL
  6. tada!

i acutally had really big problems to find the wsdl at first, but now its kinda logic 🙂

deploying asp.net mvc

the goal of every application is to get deployed on a server or another computer. sometime its really easy and logic to do so and sometimes its easy and logic, but you really mess up…

first of all asp.net mvc needs some dlls in the GAC… but normally installing stuff on servers is not really the good way… goal should be, to install as less as possible, or at least good controlable… and in my case, its a windows server… so controlable is not really a word to describe it… but on the other hand, im just a developer and not a server admin… so i might be wrong…

to avoid installing stuff in the GAC for asp.net mvc, just simply add the dlls to the bin folder and voila… every thing is good 🙂 you need to add following dlls:

  • Microsoft.Web.Infrastructure
  • System.Web.Helpers
  • System.Web.Mvc
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor

or just read http://drew-prog.blogspot.com/2011/01/how-to-deploy-aspnet-mvc-3-app-to-web.html

and it works… or lets say here ends the easy and logic part 🙂

in my case, it didnt work… why? we use nant to deploy out application and we do not publish the .config files. makes sence, because the web.config contains server information and is unique for a server… but the views folder has an own, mvc-based web.config and a packages.config, which might be used as well… so i or lets say the deploy script messed up 😛

asp.net username

need the username of the current user in an asp.net website? simple:

string username = User.Identity.Name.ToString();

ok it actually not the user name, but the domain user account name… works with windows authentification…