Applying Order to JSON Data When Rendering to a Page

It is very convenient to pass JSON data from client to server and between different server-side components. However, JSON data is an inherently unordered data structure and in many cases a developer will want to render a list of items in some specified order.

One way that I found to do so is to append an integer to the keys in your data when generating the JSON data. For example, you might be iterating through a database result set and want to maintain the order in which the result was returned. When adding your keys append ‘_n’ to the string.

Then, when you access the JSON data (with JavaScript, and jquery, and assuming a reference to your JSON data is ‘data’) first get a count of the number of objects in your JSON container:

$counter = 0;

$.each(data, function(key, value) {

? $counter++;

alert(key + ‘: ‘ + eval(value.id));

});

Now we have a count of the number of items in the object and run a for loop using the $counter variable.

for(var $rowCounter = 0; $rowCounter < $counter; rowCounter++) {

? // Access your data using the $rowCounter value

? alert(“row ” + $rowCounter + ” = ” + eval(“data.row_” + $rowCounter));

}

Opening a New Window From a Web Page with Javascript that is SEO Friendly

The days of <a href=”#” target=”_blank”></a> are long gone. The way to open a new window with a link is to do it using the javascript window.open() method via an onclick callback.

However, you still want to make your pages SEO friendly, so you don’t want to go about generating links with href=”#”. The way to do it is as follows:

Javascript:

<script>

function openNewWindow(url) {

   window.open(url);

}

</script>

HTML:

<a href=”someURL.html” onclick=”openNewWindow(‘someUrl.html’);return false;”>Link</a>

When implementing links this way, you still include the url in the href attribute making the page SEO friendly and implement the new window functionality via your javascript function. You must make sure to include return false; in your onclick attribute so that the link doesn’t load in the window from which the original click was issued.

Using bash Shell to Rename Files With Spaces

The following is a shell command that you can use to rename any file that contains spaces. Without the quotes surrounding the $file reference and the echo command it will fail.

In this example, we rename anything that is .htm to .html

for file in *.htm ; do mv “$file” “`echo “$file” | sed ‘s/\(.*\.\)htm/\1html/’`” ; done

java.lang.reflect.UndeclaredThrowableException When Attempting to Invoke a Remote Method on an EJB Singleton Instance

I am building an EJB Singleton that generates and maintains data that I want accessible to multple clients. Since the data is not based on any client interaction but based on the status of a single server-side application I went with an EJB Singleton.

When attempting to invoke a method via the Remote interface on the bean instance from a servlet in a separate .war which was returning a LinkedList of a custom Object that I wrote, I was getting the following error:

2011-03-15 13:17:59,232 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/DocumentRoot].[ServletClass]] (http-localhost%2F127.0.0.1-8080-3) Servlet.service() for servlet ServletClass threw exception: java.lang.reflect.UndeclaredThrowableException
       at $Proxy236.ejbRemoteMethod(Unknown Source)    at ServletClass.doGet(ServletClass.java:52) [:]
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [:1.0.0.Final]
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.0.0.Final]
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.0.0.Final]
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.0.0.Final]
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
       at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
       at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
       at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
       at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
       at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
       at java.lang.Thread.run(Thread.java:636) [:1.6.0_17]
Caused by: java.io.IOException
       at org.jboss.serial.persister.RegularObjectPersister.readSlotWithMethod(RegularObjectPersister.java:107) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.defaultRead(RegularObjectPersister.java:269) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.readData(RegularObjectPersister.java:241) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.readObjectDescriptionFromStreaming(ObjectDescriptorFactory.java:412) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.objectFromDescription(ObjectDescriptorFactory.java:82) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.DataContainer$DataContainerInput.readObject(DataContainer.java:845) [:6.0.0.Final]
       at org.jboss.serial.io.MarshalledObjectForLocalCalls.get(MarshalledObjectForLocalCalls.java:60) [:6.0.0.Final]
       at org.jboss.ejb3.remoting.IsLocalInterceptor.marshallOrPass(IsLocalInterceptor.java:117) [:1.7.17]
       at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:91) [:1.7.17]
       at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:75) [:1.7.17]
       at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.GA]
       at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62) [:1.0.1.GA]
       at $Proxy181.invoke(Unknown Source)    at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:185) [:1.0.11]
       … 23 more
Caused by: java.lang.reflect.InvocationTargetException
       at sun.reflect.GeneratedMethodAccessor409.invoke(Unknown Source) [:1.6.0_17]
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.6.0_17]
       at java.lang.reflect.Method.invoke(Method.java:616) [:1.6.0_17]
       at org.jboss.serial.persister.RegularObjectPersister.readSlotWithMethod(RegularObjectPersister.java:103) [:6.0.0.Final]
       … 36 more
Caused by: java.io.IOException
       at org.jboss.serial.persister.RegularObjectPersister.readSlotWithMethod(RegularObjectPersister.java:107) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.defaultRead(RegularObjectPersister.java:269) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.readData(RegularObjectPersister.java:241) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.readObjectDescriptionFromStreaming(ObjectDescriptorFactory.java:412) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.objectFromDescription(ObjectDescriptorFactory.java:82) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.DataContainer$DataContainerInput.readObject(DataContainer.java:845) [:6.0.0.Final]
       at org.jboss.serial.persister.ObjectInputStreamProxy.readObjectOverride(ObjectInputStreamProxy.java:68) [:6.0.0.Final]
       at java.io.ObjectInputStream.readObject(ObjectInputStream.java:362) [:1.6.0_17]
       at java.util.HashMap.readObject(HashMap.java:1047) [:1.6.0_17]
       … 40 more
Caused by: java.lang.reflect.InvocationTargetException
       at sun.reflect.GeneratedMethodAccessor408.invoke(Unknown Source) [:1.6.0_17]
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [:1.6.0_17]
       at java.lang.reflect.Method.invoke(Method.java:616) [:1.6.0_17]
       at org.jboss.serial.persister.RegularObjectPersister.readSlotWithMethod(RegularObjectPersister.java:103) [:6.0.0.Final]
       … 48 more
Caused by: org.jboss.serial.exception.SerializationException: Could not create instance of org.jboss.logmanager.log4j.BridgeLogger – org.jboss.logmanager.log4j.BridgeLogger
       at org.jboss.serial.classmetamodel.ClassMetaData.newInstance(ClassMetaData.java:342) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.readData(RegularObjectPersister.java:239) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.readObjectDescriptionFromStreaming(ObjectDescriptorFactory.java:412) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.objectFromDescription(ObjectDescriptorFactory.java:82) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.DataContainer$DataContainerInput.readObject(DataContainer.java:845) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.readSlotWithFields(RegularObjectPersister.java:353) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.defaultRead(RegularObjectPersister.java:273) [:6.0.0.Final]
       at org.jboss.serial.persister.RegularObjectPersister.readData(RegularObjectPersister.java:241) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.readObjectDescriptionFromStreaming(ObjectDescriptorFactory.java:412) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.objectFromDescription(ObjectDescriptorFactory.java:82) [:6.0.0.Final]
       at org.jboss.serial.objectmetamodel.DataContainer$DataContainerInput.readObject(DataContainer.java:845) [:6.0.0.Final]
       at org.jboss.serial.persister.ObjectInputStreamProxy.readObjectOverride(ObjectInputStreamProxy.java:68) [:6.0.0.Final]
       at java.io.ObjectInputStream.readObject(ObjectInputStream.java:362) [:1.6.0_17]
       at java.util.LinkedList.readObject(LinkedList.java:981) [:1.6.0_17]
       … 52 more
Caused by: java.lang.InstantiationException: org.jboss.logmanager.log4j.BridgeLogger
       at java.lang.Class.newInstance0(Class.java:357) [:1.6.0_17]
       at java.lang.Class.newInstance(Class.java:325) [:1.6.0_17]
       at org.jboss.serial.classmetamodel.ClassMetaData.newInstance(ClassMetaData.java:334) [:6.0.0.Final]
       … 65 more

After some fairly extensive experimentation I determined that the error was the result of a composed instance org.apache.log4j.Logger. Even though I included the same version of the log4.jar file in both the ejb.jar and the .war file it seems as though the class loader was having problems marshalling the Logger instance.

Had I the time to fully get it sorted out so that I could use the Logger instance in the custom class that I was returning I would, but alas . . . the rest of the project awaits.

Updating the URL of a Subversion Repository in a Currently Checked Out Copy of Your Repository

Let’s say you have a server who’s IP address or domain name changes on which you have a Subversion repository. Furthermore, you have checked out the copy over an ssh connection using svn+ssh.

If you have checked-out copies of your repo, and the path to the repo on the file system hasn’t changed, but only the IP or domain name has changed you need to issue the following command in the root of your checked out copy:

svn switch –relocate svn+ssh://user@old.domain/ svn+ssh://user@new.domain/

Authenticate to the new machine.

svn update

Authenticate again.

Done.

Setting the Screen Resolution of a Linux Guest in VMWare vSphere Client

I am currently working with virtual machines via Vmware vSphere on a Windows 2008 server. Initially I was unable to get the guest to render full screen and did not have all of the screen resolutions that I would have thought I should. What I was trying to do was have the guest Linux (in this case RedHat Enterprise Linux 6) run full-screen. It was not as straight forward as setting the screen resolution in the VM guest settings dialog.

After much trial and error the way to do it is to crank up the amount of ram allocated for the virtual video card. The guest will then have enough resources to render at full screen and you will have a wide array of screen resolution choices.

Accessing an EJB 3.1 Singleton Bean with @Startup from a Servlet in the Same JVM Instance

I am building a project whereby I have a Singleton Bean that I want to fire up when the .jar is deployed in the EJB container, thus I have it annotated as follows:

package com.ryanchapin.ejbTest

@Startup
@Singleton

public class SingletonTest{}

Of course, I have the requisite Local and Remote interfaces set, up, I’ve just left that off for brevity. I am attempting to do a JNDI lookup, from a Servlet in the same JVM using the Local interface which results in:

22:14:31,481 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TestWebProject].[ChartData]]
Allocate exception for servlet TestServlet: java.lang.ClassCastException:
$Proxy188 cannot be cast to com.ryanchapin.ejbTestLocal
?at com.ryanchapin.TestServlet<init>(TestServlet.java:51) [:]
?at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)[:1.6.0_17]
?at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)[:1.6.0_17]
?at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)[:1.6.0_17]
?at java.lang.reflect.Constructor.newInstance(Constructor.java:532)[:1.6.0_17]
?at java.lang.Class.newInstance0(Class.java:372) [:1.6.0_17]
?at java.lang.Class.newInstance(Class.java:325) [:1.6.0_17]
?at org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:280)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:264)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1162)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:955)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188)[:6.0.0.Final]
?at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)[:6.0.0.Final]
?at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)[:6.0.0.Final]
?at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)[:1.1.0.Final]
?at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)[:1.1.0.Final]
?at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)[:6.0.0.Final]
?at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)[:6.0.0.Final]
?at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)[:6.0.0.Final]
?at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)[:6.0.0.Final]
?at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)[:6.0.0.Final]
?at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)[:6.0.0.Final]
?at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)[:6.0.0.Final]
?at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)[:6.0.0.Final]
?at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)[:6.0.0.Final]
?at java.lang.Thread.run(Thread.java:636) [:1.6.0_17]

When I lookup the Remote interface JNDI name in the Servlet it works just fine. This only occurs if I have the bean annotated with @Startup. If I remove the @Startup annotation I can access the EJB via the local interface without a problem.

Perhaps a bug in Jboss-6.0.0, I’m not sure. For the time-being I am just going to use the remote interface.

?

Disabling JavaScript Validation in Eclipse Helios 3.6.1

The Helios JavaScript validator does not seem to like the jquery-1.5.min.js (or for that matter a number of other know good js libraries). As a result Eclipse complains a lot when building a project.

The quick fix that I found was to disable JavaScript validation under the Builder preferences, for your project:

Project/Properties/Builders

Simply, uncheck the “JavaScript Validator” and you should be all set.