jump to navigation

SSIS, C#, and Class Libraries February 4, 2010

Posted by Phill in Other Stuff.
Tags: , , , ,
comments closed

The past couple of weeks I’ve not been working in Java, I’ve actually been using SQL Server Integration Services. We are in the process of converting an old C++ based application to Java, with a SQL Server backend. Some of the old export processes that were previously written in C++ we have decided to re-write using SSIS.

I won’t bore you with the details, but I do want to blog about the way C# is used in SSIS. You can create ‘Script Tasks’ which basically run a piece of code for you. You can write it in VB.NET or C#.

Due to the particular export I was running, a lot of the logic needed to go into the C# code. The issue is, SSIS doesn’t make it easy for you to re-use code: essentially, when you create a script task you are creating a whole new C# project. This isn’t good.

What I did (after taking some advice from Stack Overflow), is create a class library. Being new to SSIS and the world of C# in general, I had to do a fair bit of trial and error to get it working. The best tutorial I found was here. Those steps were enough to get me started.

There are a few other things to mention as well:

  • For some reason, SSIS 2008 seems to be stuck in the world of .NET 2.0. I don’t know why, but that is why you have to copy the .dll to the .NET 2.x folder. You will probably also need to set the project properties (of your class library project) to .NET 2.0.
  • There seem to be two different ways of accessing SSIS variables from within a script, depending on whether your script is part of a Data Flow Task or not. If it’s part of a Data Flow Task, you access them via the ‘Variables’ member – easy enough. (Although remember to set them up as read-only or read-write variables in the task properties first). If your script task isn’t part of a data flow task, you will need to use the Dts variable. This confused me at first!
  • For some reason, rebuilding the Class Library package in Visual Studio seems to mean the Script Tasks using it also need to be recompiled. Update: I’ve found out why – if the version increments, you will need to rebuild the dependent projects. To stop this, you just need to set the class library version to be constant. This was actually the default in my project, but I’d changed it! You can do it from project properties -> Application -> Assembly Information dialog.

In general, it’s been a learning experience – worth doing though! I will hopefully update a bit more when I’ve had a chance to use it more…

Advertisements

MyEclipse and NullPointerExceptions December 3, 2009

Posted by Phill in Other Stuff.
Tags: , ,
comments closed

Quick one this time round. If you are using the MyEclipse editor for JSF / HTML files (in my case the MyEclipse HTML editor) and a dialog keeps popping up: “java.lang.NullPointerException. See error log for more details”, you may wish to refresh your workspace.

I kept getting this error and it turns out it was a problem with my workspace not being in sync with the files on disk. A refresh did the trck. I don’t know why this results in a NullPointerException, but there you go!

Spot the bug… October 28, 2009

Posted by Phill in Other Stuff.
Tags: , ,
comments closed

Have you ever written something really stupid but not noticed it for ages?

Witness this statement of mine I wrote a month or so ago. The point of the code is to append a prefix to an existing description, or the empty string if the description is null.

description = prefix + " " + description == null ? "" : description;

Can you spot the error?

It turns out that Java was looking at this statement like this:

description = (prefix + " " + description) == null ? "" : description;

D’oh! Not what I intended! I changed the statement to put brackets round the description == null ternary statement and it seems to work.

Just goes to show, brackets are generally a good idea when making statements like this – it’s too easy to make mistakes with operator precedence otherwise!

Java, LDAP and Active Directory April 20, 2009

Posted by Phill in Other Stuff.
Tags: , ,
comments closed

I was trying to connect up to a Microsoft Active Directory server using Jxplorer. I kept getting the error message:

LdapErr: DSID-0C090334, comment: AcceptSecurityContext error

It seems that in the “User DN” field I was entering the username. This doesn’t work. Instead, you must use the fully qualified name, i.e. username@domain.com.

Just a quick post, hopefully this will be useful to someone else!

Creating JAR files with dependencies February 16, 2009

Posted by Phill in Other Stuff.
Tags: , ,
comments closed

File this one under “spend a few minutes Googling without success!”

If you’re trying to package up a Java application into one JAR file for easy deployment – including all dependency JAR files – there aren’t many options.

There’s a plugin for Eclipse called “Fat Jar” which I haven’t tried out. I’ve just discovered a tool called One-Jar, which does the trick. It’s simple to use. In my case, using Ant, it was a matter of:

  • Download the One-Jar JAR file.
  • Run it (using java -jar) to extract the contents
  • Add the one jar ant task plugin .jar file to my ant lib folder
  • Import the one-jar-ant-task.xml build file into my build file
  • Create a manifest.mf file with the One-Jar-Main-Class set
  • Add in the <one-jar> task to my existing build.xml file as per the instructions.
  • And you’re done!

The ant task does a lot of the work for you so it’s nice and easy. I’d recommend it, if you’re looking for a quick way to include dependencies in a single JAR.

Firebug ‘Gotcha’ January 5, 2009

Posted by Phill in Other Stuff.
comments closed

I was debugging a few AJAX requests recently which didn’t seem to be working. I used Firebug to check exactly what was being sent back in the request.

After about an hour to-ing and fro-ing, eventually using an HTTP Debugging proxy, I found out what the problem was: Firebug doesn’t send the submitted POST data when you need to send another request to examine the response. Or at least, it didn’t for me (for some reason) – it turns out the AJAX request was working fine, I just couldn’t work out why my POST data wasn’t showing up!

Can’t believe I wasted so much time on such a stupid thing, but there you go!

Spring Transactions across multiple datasources September 16, 2008

Posted by Phill in Other Stuff, Spring.
Tags: , , , ,
comments closed

Update: Since writing this post I have discovered problems with the Enhydra connection pool, and as such I do not recommend using it. I recommend finding another way to accomplish what you want to do.

We have an application here which connects to two different data sources. I was having problems getting the application to be transactional using Springs @Transactional annotation. The problem is, the Transactional annotation does not let you specify a transaction manager – and one transaction manager can only manage one DataSource. Or so I thought.

It turns out that it is possible, using JTA – and JOTM.

The configuration you need is pretty minimal. In your application context XML file, you need to put the following:

<bean id="jotm"
class="org.springframework.transaction.jta.JotmFactoryBean" />

<bean id="innerDataSource"
class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="${dataSource.driver}" />
<property name="url" value="${dataSource.url}" />
<property name="user" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>

<bean id="dataSource"
class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource" ref="innerDataSource" />
<property name="user" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>

<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>

(You also need to set up your libraries to inculde JOTM and the XA Data Sources – which can be got here)

This is for the first DataSource. Your second DataSource should be set up as per the above, except that you don’t need to repeat the jotm or transactionManager beans.

Provided that you then set up your transactions as annotation-driven – and actually use a few annotations – you should find your methods are transactional across multiple dataSources!

You may also find it helps to read a couple of articles on the subject, two which I found helpful are here and here.

I’m a hopeless addict! September 16, 2008

Posted by Phill in Other Stuff.
Tags:
comments closed

I’m addicted to StackOverflow. It’s a new website which has been started up by Joel Spolsky (of Joel on Software fame) and Jeff Atwood (of Coding Horror – the blog – fame). It’s basically a sort of digg / reddit / Wikipedia style site where you can ask and answer questions about programming.

It’s a really good idea – but the problem is, it’s so damn addictive! I just love getting those points and badges… it’s kind of like being in the Scouts. Except that I never heard of anyone who was addicted to getting badges in the Scouts (maybe there were some, I don’t know!)

Anyway, it’s well worth checking out and I would suggest is worthy of adding to your bookmarks!

Creating a Web Service with Spring-WS July 16, 2008

Posted by Phill in Other Stuff, Spring.
Tags: , , , , ,
comments closed

Recently, I’ve had to use Spring-WS to expose a couple of web services. I’d previously exposed web services using CXF (or XFire as it was then), but that worked a bit differently: you defined the beans you wanted to expose as Web Services, and XFire would generate all the WSDL for you.

I wanted to try out contract-first web service development, hence me using Spring-WS. (There are a variety of good reasons to use contract-first development, rather than generating your interface from Java.)

Anyway, to cut a long story short, here are a few brief notes about my experiences, hopefully this will help out anyone else who wants to start developing in Spring-WS 🙂

  • Before you even start reading through the Spring-WS documentation, read up about XSD. Seriously, if you don’t know XSD, you will need to. Even a basic understanding will help greatly. This document by the W3C is an excellent place to start.
  • The Spring-WS Tutorial isn’t very helpful, in that it only defines a web service which has a request (i.e., no response is required). If you want the Web Service to have a response, you need to also include a “{Name}Response” type in your XSD. In other words, if their example should be modified to include a response to a HolidayRequest, you should include a HolidayResponse element.
  • I used JAXB as an O/X mapper. It has an Ant task which will generate your Java classes from an XSD, which I found very useful. It’s also dead easy to use with Spring-WS.
  • If you’re not using Spring MVC, you’ll need to set up a MessageDispatcherServlet. I bound mine to the path /services/* – however, there are a couple of ‘gotchas’ here:
  1. You’ll probably want to set the transformWsdlLocations parameter to ‘true’, so you can specify the web service path as a relative URL.
  2. The servlet, being a Spring MVC style servlet, looks for an application context .xml file in /WEB-INF/{servlet-name}-servlet.xml. As far as I know, you must include this. It’s not really a problem because it helps separate out concerns (i.e. defining all your Web Service beans in one place) – and also, you can reference all your beans defined elsewhere. However, it’s just something to be aware of.
  3. Remember I said I bound my servlet to /services/*? When you define your endpoints, you must include that prefix in the endpoint. So, if the URL to your service is /ContextPath/services/MyService/, your “locationUri” property of the endpoint will be /services/MyService/. Not /MyService/. It took me a while to figure that out!
  • Finally, you’ll need several beans to define a Web Service. Although you can get this from the Spring-WS documentation, this is a list in the one place:
    • A Wsdl Definition (such as DefaultWsdl11Definition) – which will expose your WSDL.
    • An XSD Schema definition (such as SimpleXsdSchema) – if you’re automatically generating WSDL from your XSD.
    • An endpoint mapping (such as PayloadRootQNameEndpointMapping), which will map from the incoming requests onto your endpoints.
    • Any endpoints you want to have setup, i.e. one per web service you want to expose
    • If you’re using a Marshaller (such as JAXB), you will also need the marshaller / unmarshaller configured, for example Jaxb2Marshaller.

*phew* I think that’s just about everything.

I realise all that is probably not very clear, it’s just a few notes from the little issues I’ve run into over the past few days trying to get the web service to work. In time I might get around to organising it into a more coherent tutorial 🙂

Why you should validate input January 17, 2008

Posted by Phill in Other Stuff.
Tags: ,
comments closed

If there was ever a lesson as to why you should always validate input, this is it.

The reason I am re-posting this (I think it’s been doing the rounds on the internet) is because I think every software developer should read it and take it to heart – the more people know about it, the less chance there is of this happening again.

Oh, and a (belated) happy new year to you all!