Categories
discuss

Is there a “Group Box” equivalent in Java Swing?

Trying to build a GUI application in Java/Swing. I’m mainly used to “painting” GUIs on the Windows side with tools like VB (or to be more precise, Gupta SQLWindows… wonder how many people know what that is ;-)).

I can’t find an equivalent of a Group Box in Swing…

With a group box, you have a square box (usually with a title) around a couple of related widgets. One example is a group box around a few radio buttons (with the title explaining what the radio buttons are about, e.g. Group Box entitled “Sex” with “Male” and “Female” radio buttons).

I’ve searched around a bit… the only way I found was to add a sub-pane, set the border on the sub-pane and then add all the widgets in the “group” to the sub-pane. Is there a more elegant way to do that?

Answer

Create a JPanel, and add your radiobuttons to it. Don’t forget to set the layout of the JPanel to something appropriate.

Then call panel.setBorder(BorderFactory.createTitledBorder(name));

Categories
discuss

Disabling seam’s redirect filter

I’m doing a project in seam that requires restful URLs. I have a view that is mapped to /group/{group}/{locale}. On that page I have a list of so called messages. Each message has a button to save changes to the message. This is linked to an action bean like this:

<h:commandButton type="submit" value="Save Changes" action="#{groupAction.update}" />

Each message has an anchor, so /group/{group}/{locale}#{id} can be used to make the browser go to that anchor. This is why I need a redirect after the POST:

<page view-id="/group.xhtml">
  <rewrite pattern="/group/{group}/{locale}"/>

  <param name="group" value="#{groupAction.group}"/>
  <param name="locale" value="#{groupAction.locale}"/>

  <navigation from-action="#{groupAction.update}">
    <redirect view-id="/group.xhtml?group=#{group}&locale=#{locale}##{id}"/>
  </navigation>
</page>

Also I have the following redirect rule (UrlRewriteFilter) to get to the proper RESTful URL:

<outbound-rule>
  <from>^/group.xhtml?group=([.w]+)&locale=([.w]+)#([.w]+)?cid=(d*)$</from>
  <to type="temporary-redirect" last="true">/group/$1/$2#$3</to>
</outbound-rule>

I strip the conversationId here. This has been tested an works. However seam still appends a ‘?conversationId={cid}’. So what’s the problem? Well, imagine a URL like ‘/group/{group}/{locale}#{id}?conversationId={cid}’. Oviously the browser doesn’t like this and will not automatically go to that anchor.

I did some research and discovered my problem in the seam documentation:

29.1.4.2. Conversation propagation with redirects This filter allows Seam to propagate the conversation context across browser redirects. It intercepts any browser redirects and adds a request parameter that specifies the Seam conversation identifier. The redirect filter will process all requests by default, but this behavior can also be adjusted in components.xml:

<web:redirect-filter url-pattern="*.seam"/>

I don’t need the redirect-filter and I tried putting something invalid in the url-pattern to “disable” the filter. However that didn’t work. So my question is now:

How do I disable the redirect-filter in seam?

I can’t find the answer. The seam documentation talks about disabling it in web.xml, but my attempts have not been succesful yet.

Answer

I’ve worked out where the unwanted conversationId query string parameter is coming from.

The <redirect/> results in a call to org.jboss.seam.faces.FacesManager.redirect(String viewId, Map<String, Object> parameters, boolean includeConversationId)

This is called from the following code in org.jboss.seam.faces.Navigator which sets includeConversationId to true:

  FacesManager.instance().redirect(viewId, parameters, true);

I cannot see any way to avoid this, so the fix/workaround is to do the redirect programmatically in the action method with:

  FacesManager.instance().redirect(viewId, parameters, false);
Categories
discuss

Grails, Inserting lots of data using withTransaction results in OutOfMemoryError

I’m using Grails 1.1 beta2. I need to import a large amount of data into my Grails application. If I repeatedly instantiate a grails domain class and then save it, the performance is unacceptably slow. Take for example importing people from a phone book:

for (each person in legacy phone book) {
    // Construct new Grails domain class from legacy phone book person
    Person person = new Person(...)
    person.save()
}

This turns out be painfully slow. Someone on the Grails mailing list suggest batching up saves in a transaction. So now I have:

List batch = new ArrayList()
for (each person in legacy phone book) {
    // Construct new Grails domain class from legacy phone book person
    Person person = new Person(...)
    batch.add(person)
    if (batch.size() > 500) {
        Person.withTransaction {
            for (Person p: batch)
                p.save()
            batch.clear()
        }
    }
}
// Save any remaining
for (Person p: batch)
    p.save()

This works must faster, at least initially. Each transaction saves 500 records. As time goes on, the transactions take longer and longer. The first few transactions takes about 5 seconds, then it just creeps from there. After about 100 transactions, each one takes over a minute, which is once again unacceptable. Worse is that eventually Grails will eventually run out of Java heap memory. I can increase the JVM heap size, but that just delays the OutOfMemoryError exception.

Any ideas why this is? It’s like there some internal resource not being released. The performance gets worse, memory is being held on to, and then eventually the system runs out of memory.

According to the Grails documentation, withTransaction passes the closure to Spring’s TransactionStatus object. I couldn’t find anything in TransactionStatus to close/end the transaction.

Edit: I’m running this from Grails’ Console (grails console)

Edit: Here’s the out of memory exception:

Exception thrown: Java heap space

java.lang.OutOfMemoryError: Java heap space
    at org.hibernate.util.IdentityMap.entryArray(IdentityMap.java:194)
    at org.hibernate.util.IdentityMap.concurrentEntries(IdentityMap.java:59)
    at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:113)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)

Answer

This is a common issue with all hibernate applications and it is caused by the growth of the hibernate session. I’m guessing that the grails console holds a hibernate session open for you in a similar way to the ‘open session in view’ pattern that I know it uses in for normal web requests.

The solution is to get hold of the current session and clear it after each batch. I’m not sure how you get hold of spring bean using the console, normally for controllers or services you just declare them as members. Then you can get the current session with sessionFactory.getCurrentSession(). In order to clear it just call session.clear(), or if you what to be selective use session.evict(Object) for each Person object.

for a controller/service:

class FooController {
    def sessionFactory

    def doStuff = {
        List batch = new ArrayList()
        for (each person in legacy phone book) {
            // Construct new Grails domain class from legacy phone book person
            Person person = new Person(...)
            batch.add(person)
            if (batch.size() > 500) {
                Person.withTransaction {
                    for (Person p: batch)
                        p.save()
                    batch.clear()
                }
                // clear session here.
                sessionFactory.getCurrentSession().clear();
            }
        }
        // Save any remaining
        for (Person p: batch)
            p.save()
        }
    }
}

Hope this helps.

Categories
discuss

jar resources in jnlp are not signed by the same certificate

I’ve been working with web start for a couple years now and have experience with signing the jars and what not. I am taking my first attempt at deploying a RCP app with web start and though I have in fact signed all of the jars with the same certificate I keep getting this error: ‘jar resources in jnlp are not signed by the same certificate’

Has anyone else came across this? If so, any ideas on how to fix?

Answer

When I had similar problems after checking the jars it turned out that some 3rd party jar was signed by someone else.

You should create a separate jnlp file for the jars signed by the other certificate and read this jnlp from your jnlp file:

<resources>
  ...
  <extension name="other" href="other.jnlp"/>
</resources>

Here or here you can find an example.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..