title

fire chief's random developer tidbits

Tuesday, June 24, 2014

Little glassfish gem for redirecting pages

http://notsomany.wordpress.com/2010/06/29/glassfish-v2-1-1-site-redirect/

This is nice for development or small setups because you don't have to set up anything else in the way of firewalls or httpd redirects.

Still works on Glassfish 4.

Wednesday, July 31, 2013

Jaxb name conflict with underscores

<xs:element name="_SortFields" nillable="true" type="tns:_SortFields"/>

will conflict with

<xs:element name="SortFields" nillable="true" type="tns:SortFields"/>

giving the error:

Two declarations cause a collision in the ObjectFactory class.
  line 300 of file:blah.xsd

(Related to above error) This is the other declaration.   
  line 385 of file:blah.xsd

because it is removing underscores from names by default.

A bindings file like this will fix it:
<jxb:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" 
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:globalBindings underscoreBinding="asCharInWord">
        <xjc:simple/>
        <xjc:serializable />
    </jxb:globalBindings>
</jxb:bindings>

Thursday, July 25, 2013

Spring security default https port 8443 and Glassfish

Glassfish and Spring Security have different default ports for https traffic 8181 vs 8443.  To get that working right in a development environment I did the following:

pom.xml:
    <properties>
        <http.port>8080</http.port>
        <http.port>8181</http.port>
    </properties>

    <build>
        <resources>
             <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </reources>
...

       <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/resources/WEB-INF/context</directory>
                            <targetPath>WEB-INF/context</targetPath>
                            <includes>
                                <include>applicationContext.xml</include>
                                <include>applicationContext-security.xml</include>
                                <include>datasourceContext.xml</include>
                            </includes>
                        </resource>
...
                


    <profiles>
        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <http.port>80</http.port>
                <https.port>443</https.port>
            </properties>
        </profile>


applicationContext-security.xml:
    <http>
        <intercept-url pattern="/" access="permitAll" requires-channel="https"/>
        <intercept-url pattern="/**" access="hasRole('USER')" requires-channel="https" />
        <port-mappings>
            <port-mapping http="${http.port}" https="${https.port}"/>
        </port-mappings>
    </http>

Part of the answer came from here about moving context files to resources though I am using Netbeans, so I didn't have to worry about m2e problems he had.

Wednesday, July 24, 2013

Deleting child entity when the child is removed from the parent collection.

orphanRemoval on JPA relationships is very useful, and possibly misnamed.  It doesn't handle orphan removal, but it handles removing deleted child entities meaning that in JPA 2.0 you can remove an object in a collection and then persist the parent and it will actually remove the deleted child entity from the database.

http://docs.oracle.com/javaee/6/api/javax/persistence/OneToMany.html#orphanRemoval

I think the name orphanRemoval is strange.  It's a terrible analogy, but when you have a parent duck and its ducklings, if you remove a duckling (kill it) it doesn't become an orphan, it just becomes dead.

Tuesday, May 21, 2013

Logging soap contents with JAX-WS


Looking around the web, I found various system properties that were suggested for getting JAX-WS to dump the soap contents to system out.  Different ones work for different situations.  While I can't name all the situations, here are the properties all in one place.  Set the appropriate one to "true".  Sometimes the "brute force" solution of putting them all in, then trying one half or the other (binary search) might be the fastest way to figure it out without driving yourself crazy.

These can work when not using the internal/embedded JAX-WS in the JVM (for example running within Glassfish with a full Metro stack in place):
com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump
com.sun.xml.ws.transport.http.HttpAdapter.dump
com.sun.xml.ws.transport.local.LocalTransportPipe.dump

These will work when using the Internal/embedded JAX-WS (for example running a single main class within Netbeans).
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump
com.sun.xml.internal.ws.transport.http.HttpAdapter.dump
com.sun.xml.internal.ws.transport.local.LocalTransportPipe.dump


This was the one that actually worked in my situation, running a standalone JAX-WS client within Netbeans:
com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump

Thursday, November 29, 2012

Hibernate SQLQuery and Oracle CLOB (long text)

This is possibly very obvious but it took me a while to find the answer.  Using existing native queries (SQLQuery) in hibernate I have a CLOB column I needed to map using a result set transformer.  Note when you use one addScalar() you must map every column with addScalar() that you want returned.  Use the org.hibernate.type.TextType (hibernate 3.5, it looks like hibernate 4 would be TextType.TEXTTYPE).

String sql = "SELECT BLAH_ID as id, FOO_DETAIL as detailText FROM blah";

Query query =
            session.createSQLQuery(sql)
            .addScalar("id")
            .addScalar("detailText", new TextType())
            .setResultTransformer(Transformers.aliasToBean(SomePojo.class));

class SomePojo {
    private Long id;
    private String detailText;

    ...getters and setters...
}
Now the real question is why did they do this native hibernate inside a Spring JpaRepository and not use NamedParameterJDBCTemplate with BeanPropertyRowMapper? It's very similar but I find the Spring style cleaner. Time to refactor.