Friday, January 3, 2014

JSON Schema to Model Converter


JSON schema/Example to model converter
http://www.jsonschema2pojo.org/

This supports Jackson and Google gson JSON libraries.

JSON Viewer
http://jsonviewer.stack.hu/


Example: for my project JSON example
{
  "projectName":"TestProject",

  "properties": {
    "createdBy": "Kondal Kolipaka",
    "createdTime": "21-01-2013",
    "lastModifiedTime":"21-02-2013"
  }
}


Generated Model for above Project JSON example:

package com.kk;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"projectName",
"properties"
})
public class Project {

@JsonProperty("projectName")
private String projectName;
@JsonProperty("properties")
private Properties properties;
private Map<StringObject> additionalProperties = newHashMap<StringObject>();

@JsonProperty("projectName")
public String getProjectName() {
return projectName;
}

@JsonProperty("projectName")
public void setProjectName(String projectName) {
this.projectName = projectName;
}

@JsonProperty("properties")
public Properties getProperties() {
return properties;
}

@JsonProperty("properties")
public void setProperties(Properties properties) {
this.properties = properties;
}

@JsonAnyGetter
public Map<StringObject> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(String nameObject value) {
this.additionalProperties.put(namevalue);
}

}
-----------------------------------com.kk.Properties.java-----------------------------------

package com.kk;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({
"createdBy",
"createdTime",
"lastModifiedTime"
})
public class Properties {

@JsonProperty("createdBy")
private String createdBy;
@JsonProperty("createdTime")
private String createdTime;
@JsonProperty("lastModifiedTime")
private String lastModifiedTime;
private Map<StringObject> additionalProperties = newHashMap<StringObject>();

@JsonProperty("createdBy")
public String getCreatedBy() {
return createdBy;
}

@JsonProperty("createdBy")
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

@JsonProperty("createdTime")
public String getCreatedTime() {
return createdTime;
}

@JsonProperty("createdTime")
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}

@JsonProperty("lastModifiedTime")
public String getLastModifiedTime() {
return lastModifiedTime;
}

@JsonProperty("lastModifiedTime")
public void setLastModifiedTime(String lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
}

@JsonAnyGetter
public Map<StringObject> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperties(String nameObject value) {
this.additionalProperties.put(namevalue);
}

}

As we can see it also generates getAdditionalProperties(), this would be very much useful later point of time if we wanted to send some additional information without really modifying the existing structure.

Thursday, January 2, 2014

org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start()

Today morning, one of my colleague was reported that, his eclipse is not launching and It throws below error.

MESSAGE An error occurred while automatically activating bundle
org.eclipse.core.resources (26).
!STACK 0
org.osgi.framework.BundleException: Exception in
org.eclipse.core.resources.ResourcesPlugin.start() of bundle org.eclipse.core.resources.
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.s tartActivator
(BundleContextImpl.java:1028)

I have verified all the plugins, and could not see any issue with them. So where is the problem then?

I could solve the problem with Option (2).

Option 1: Try launching eclipse in clean mode. This did not work in my case, it was throwing an error before it asks for workspace selection.
> eclipse -clean

Option 2: Delete .metadata/.plugins/org.eclipse.core.resources/.snap file workspace directory.

Option 3: Most of the time option (2) will work, if it's not working, try to delete all files from .metadata/  folder.

After launching your eclipse, you will not find your old projects and workspace preference settings, you need to re-import into eclipse workspace and need to configure eclipse preferences.
  
Why this would have happened?
As per my understanding, this happens if you closed your eclipse forcefully, shutdown the system forcefully before eclipse closes or some files in workspace metadata would have been modified.
  
What is this .snap files contain ?
*.snap files represent the changes in workspace state of the IDE during the runtime. This is mostly for eclipse crash recovery plan. When a crash happens these files are used to recover the state of eclipse workspace.

While Eclipse is running, information about what has changed in the workspace is incrementally logged into various "snapshot" files (including .snap). On normal Eclipse shutdown, the complete workspace state is saved and the .snap files are deleted. When Eclipse crashes, the snapshot files are used during the next startup to recover from the crash.





Enable Eclipse verbose for troubleshooting

Have you ever wanted to trace what happens when you start an eclipse, or wanted to check what are the classes are loading during start up or while performing certain action.

Just to mention my use case, I was facing the below problem,

java.lang.LinkageError: loader constraint violation: when resolving method "com.kk.MUtils.logDebug(Lorg/apache/log4j/Logger;Ljava/lang/String;)V" the class loader (instance of java/net/FactoryURLClassLoader) of the current class, com/kk/common/KHttpServletRequestWrapper, and the class loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) for resolved class, com/kk/MUtils, have different Class objects for the type com.kk.MUtils.logDebug(Lorg/apache/log4j/Logger;Ljava/lang/String;)V used in the signature


From the above error, we can understand that Logger class is the issue, it was loaded multiple times by different class loaders and they could not link each other since both of them are loaded by different class loaders.

My call request involves multiples plug-ins, I wanted to check which of the plugins are loading Logger class to identify a root cause of the problem.

To get all that info, we can make use of java verbose vm parameter.

so how do we enable that ?

By default, eclipse launches with javaw.exe process, since it's a window thread so you will not be able to see any verbose messages.

Step 1:  Add below VM parameter in eclipse.ini file. By default, eclipse will have javaw.exe, modify it to java.exe
-vm
C:/KK/jdk1.6.0_32/bin/java.exe

Step 2: Pass verbose parameter during the eclipse launch.
> eclipse -verbose

This will open up a console window to display all log messages.

Write it to a file:
To write log messages to a file, use below command.
>eclipse -verbose  >mytracelog.txt

You can find this file in eclipse root directory.

You can also provide complete file system path, if required.
>eclipse -verbose  >C:\kk\logs\mytracelog.txt

You can also add verbose parameter in eclipse.ini vm parameters.
Example:
-vm
C:/KK/jdk1.6.0_32/bin/java.exe
-vmargs
-Xms128m
-Xmx512m
-verbose




Java Decompiler

Decompiler is to convert java .class files into java source files. This would be useful, If you don't have source code of a particular jar file, but you wanted to check the source of it. 

I am using below Java decompiler for last few years, and it was very much useful to me.

You can directly download:

If you are working with eclipse, please find the update site below.