Featured FREE Whitepapers

What's New Here?

junit-logo

Tomcat Context JUnit @Rule

A first draft of a JUnit @Rule that create the test context. This can be used with the Spring context rule for this post to create a complete Spring context for integration tests. import org.apache.commons.dbcp.BasicDataSource; import org.apache.log4j.Logger; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.*; import java.lang.reflect.Method; import java.sql.Driver; import java.sql.DriverManager; /** * Creates an context for tests using an Apache Tomcat server.xml. * * https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit * * @author alex.collins */ public class TomcatContextRule implements TestRule { public static final Logger LOGGER = Logger.getLogger(CatalinaContextRule.class); /** * Creates all the sub-contexts for a name. */ public static void createSubContexts(Context ctx, String name) { String subContext = ''; for (String x : name.substring(0, name.lastIndexOf('/')).split('/')) { subContext += x; try { ctx.createSubcontext(subContext); } catch (NamingException e) { // nop } subContext += '/'; } } private final File serverXml; public TomcatContextRule(File serverXml, Object target) { if (serverXml == null || !serverXml.isFile()) {throw new IllegalArgumentException();} if (target == null) {throw new IllegalArgumentException();} this.serverXml = serverXml; } public Statement apply(final Statement statement, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { createInitialContext(); try { statement.evaluate(); } finally { destroyInitialContext(); } } }; } private void createInitialContext() throws Exception { LOGGER.info('creating context'); System.setProperty(Context.INITIAL_CONTEXT_FACTORY, org.apache.naming.java.javaURLContextFactory.class.getName()); System.setProperty(Context.URL_PKG_PREFIXES, 'org.apache.naming'); final InitialContext ic = new InitialContext(); createSubContexts(ic, 'java:/comp/env'); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.parse(serverXml); // create Environment { final NodeList envs = document.getElementsByTagName('Environment'); for (int i = 0; i < envs.getLength(); i++) { final Element env = (Element)envs.item(i); // must be Element final String name = 'java:comp/env/' + env.getAttribute('name'); final Object instance = Class.forName(env.getAttribute('type')).getConstructor(String.class) .newInstance(env.getAttribute('value')); LOGGER.info('binding ' + name + ' <' + instance + '>'); createSubContexts(ic, name); ic.bind(name, instance); } } // Resource { final NodeList resources = document.getElementsByTagName('Resource'); for (int i = 0; i < resources.getLength(); i++) { final Element resource = (Element)resources.item(i); // must be Element final String name = 'java:comp/env/' + resource.getAttribute('name'); final Class<?> type = Class.forName(resource.getAttribute('type')); final Object instance; if (type.equals(DataSource.class)) { { @SuppressWarnings('unchecked') // this mus be driver? final Class<? extends Driver> driverClass = (Class<? extends Driver>) Class.forName(resource.getAttribute('driverClassName')); DriverManager.registerDriver(driverClass.newInstance()); } final BasicDataSource dataSource = new BasicDataSource(); // find all the bean attributes and set them use some reflection for (Method method : dataSource.getClass().getMethods()) { if (!method.getName().matches('^set.*')) {continue;} final String x = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); if (!resource.hasAttribute(x)) {continue;} Class<?> y = method.getParameterTypes()[0]; // might be primitive if (y.isPrimitive()) { if (y.getName().equals('boolean')) y = Boolean.class; if (y.getName().equals('byte')) y = Byte.class; if (y.getName().equals('char')) y = Character.class; if (y.getName().equals('double')) y = Double.class; if (y.getName().equals('float')) y = Float.class; if (y.getName().equals('int')) y = Integer.class; if (y.getName().equals('long')) y = Long.class; if (y.getName().equals('short')) y = Short.class; if (y.getName().equals('void')) y = Void.class; } method.invoke(dataSource, y.getConstructor(String.class).newInstance(resource.getAttribute(x))); } instance = dataSource; } else { // not supported, yet... throw new AssertionError('type ' + type + ' not supported'); } LOGGER.info('binding ' + name + ' <' + instance + '>'); createSubContexts(ic, name); ic.bind(name, instance); } } } private void destroyInitialContext() { System.clearProperty(Context.INITIAL_CONTEXT_FACTORY); System.clearProperty(Context.URL_PKG_PREFIXES); LOGGER.info('context destroyed'); } } For example: @Rule public TestRule rules = RuleChain.outerRule(new CatalinaContextRule(new File(getClass().getResource('/server.xml').getFile()), this)) .around(new ContextRule(new String[] {'/applicationContext.xml'}, this)); This code is on Github. Reference: Tomcat Context JUnit @Rule from our JCG partner Alex Collins at the Alex Collins ‘s blog blog....
oasis-saml-logo

Implementing SAML to XACML

Before Implementing SAMLThis is how a XACML request will looks like when it is arriving at PDP(Policy Decision Point) to be evaluated. <Request xmlns='urn:oasis:names:tc:xacml:2.0:context:schema:os'> <Subject> <Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:subject:subject-id' DataType='http://www.w3.org/2001/XMLSchema#string'> <AttributeValue>admin</AttributeValue> </Attribute> </Subject> <Resource> <Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:resource:resource-id' DataType='http://www.w3.org/2001/XMLSchema#string'> <AttributeValue>http://localhost:8280/services/echo/echoString</AttributeValue> </Attribute> </Resource> <Action> <Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:action:action-id' DataType='http://www.w3.org/2001/XMLSchema#string'><AttributeValue>read</AttributeValue> </Attribute> </Action> <Environment/> </Request> Basically it states who is(Subject) wanting to access which resource and what action it wants to perform on the resource. PDP trusts that request made is not altered while being sent and received, evaluates the request against existing enabled policies and reply with the decision which will be as follows. <Response> <Result ResourceId='http://localhost:8280/services/echo/echoString'> <Decision>Permit</Decision> <Status> <StatusCode Value='urn:oasis:names:tc:xacml:1.0:status:ok'/> </Status> </Result> </Response> Again there is no guarantee for the party who is using this response that this decision is not altered since sent from PDP until been received. In order achieve the security of XACML requests and responses in server to server communication SAML profile for XACML is defined by OASIS.This take the system security to a higher level by allowing the usage of fine-grained authorization provided by XACML, to be signed.After Implementing SAMLFollowing is how the previous XACML request looks like after wrapped into a XACMLAuthzDecisionQueryType, which is generated using OpenSAML 2.0.0 library which is supporting SAML profile of XACML as declared in 2004 . The diagram shows the basic structure of a XACMLAuthzDecisionQueryType.Following is a sample XACMLAuthzDecisionQuery. <xacml-samlp:XACMLAuthzDecisionQueryType InputContextOnly='true' IssueInstant='2011-10-31T06:44:57.766Z' ReturnContext='false' Version='2.0' xmlns:xacml-samlp='urn:oasis:names:tc:xacml:2.0:profile:saml2.0:v2:schema:protocol'> <saml:Issuer SPProvidedID='SPPProvierId' xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'> https://identity.carbon.wso2.org</saml:Issuer> <ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm='http://www.w3.org/2001/10/xml-exc-c14n#'/> <ds:SignatureMethod Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1'/> <ds:Reference URI=''> <ds:Transforms> <ds:Transform Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature'/> <ds:Transform Algorithm='http://www.w3.org/2001/10/xml-exc-c14n#'> <ec:InclusiveNamespaces PrefixList='ds saml xacml-context xacml-samlp' xmlns:ec='http://www.w3.org/2001/10/xml-exc-c14n#'/> </ds:Transform> </ds:Transforms> <ds:DigestMethod Algorithm='http://www.w3.org/2000/09/xmldsig#sha1'/> <ds:DigestValue>7T1ScatC2Xg7pSpjB2X9HB3EH8M=</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue>XQBUVH3j16HVm3aTFSFh5EYFyiYjn0IU4PJfXelzK6BfXp GGTBGouVJEe2Kk26sa3Yj0nEgh51pKsNWxk8xQFWdXg6/UlMkq+CaKrYj7laYlM9yGuIlEBT6t yzjIQBa8wskHeITL6tHE+G0aMa5YnTqtb+9IaJKGPIrl/K5Zn2A=</ds:SignatureValue> <ds:KeyInfo> <ds:X509Data> <ds:X509Certificate>MIICNTCCAZ6gAwIBAgIES343gjANBgkqhkiG9w0BA QUFADBVMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExFjAUBgNVBAcMDU1vdW50YWluIFZ pZXcxDTALBgNVBAoMBFdTTzIxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xMDAyMTkwNzAyM jZaFw0zNTAyMTMwNzAyMjZaMFUxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1U EBwwNTW91bnRhaW4gVmlldzENMAsGA1UECgwEV1NPMjESMBAGA1UEAwwJbG9jYWxob3N0M IGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUp/oV1vWc8/TkQSiAvTousMzOM4asB2i ltr2QKozni5aVFu818MpOLZIr8LMnTzWllJvvaA5RAAdpbECb+48FjbBe0hseUdN5Hpwvn H/DW8ZccGvk53I6Orq7hLCv1ZHtuOCokghz/ATrhyPq+QktMfXnRS4HrKGJTzxaCcU7OQI DAQABoxIwEDAOBgNVHQ8BAf8EBAMCBPAwDQYJKoZIhvcNAQEFBQADgYEAW5wPR7cr1LAdq +IrR44iQlRG5ITCZXY9hI0PygLP2rHANh+PYfTmxbuOnykNGyhM6FjFLbW2uZHQTY1jMrP prjOrmyK5sjJRO4d1DeGHT/YnIjs9JogRKv4XHECwLtIVdAbIdWHEtVZJyMSktcyysFcvu hPQK8Qc/E/Wq8uHSCo=</ds:X509Certificate> </ds:X509Data> </ds:KeyInfo> </ds:Signature> <xacml-context:Request xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'><xacml-context:Subject SubjectCategory='urn:oasis:names:tc:xacml:1.0:subject-category:access-subject' xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'><xacml-context:Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:subject:subject-id' DataType='http://www.w3.org/2001/XMLSchema#string'><xacml-context:AttributeValue>admin</xacml-context:AttributeValue></xacml-context:Attribute></xacml-context:Subject><xacml-context:Resource xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'><xacml-context:Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:resource:resource-id' DataType='http://www.w3.org/2001/XMLSchema#string'><xacml-context:AttributeValue>http://localhost:8280/services/echo/echoString</xacml-context:AttributeValue></xacml-context:Attribute></xacml-context:Resource><xacml-context:Action xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'><xacml-context:Attribute AttributeId='urn:oasis:names:tc:xacml:1.0:action:action-id' DataType='http://www.w3.org/2001/XMLSchema#string'><xacml-context:AttributeValue>read</xacml-context:AttributeValue></xacml-context:Attribute></xacml-context:Action><xacml-context:Environment xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'/> </xacml-context:Request> </xacml-samlp:XACMLAuthzDecisionQueryType> As you can see it carries lot of information related to the content of the request like who issued it , when, signature with the X509Certificate and the XACML request. Data integrity can be preserved in this way. After executing the request and gaining the response from PDP, it is also sent secured with a signature. The diagram shows the structure of a basic SAML Response.Following is a sample SAML response that carries XACML response. <samlp:Response IssueInstant='2011-10-31T06:49:51.013Z' Version='2.0' xmlns:samlp='urn:oasis:names:tc:SAML:2.0:protocol'> <saml:Issuer SPProvidedID='SPPProvierId' xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'>https://identity.carbon.wso2.org</saml:Issuer><ds:Signature xmlns:ds='http://www.w3.org/2000/09/xmldsig#'> <ds:SignedInfo> <ds:CanonicalizationMethod Algorithm='http://www.w3.org/2001/10/xml-exc-c14n#'/> <ds:SignatureMethod Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1'/> <ds:Reference URI=''> <ds:Transforms> <ds:Transform Algorithm='http://www.w3.org/2000/09/xmldsig#enveloped-signature'/> <ds:Transform Algorithm='http://www.w3.org/2001/10/xml-exc-c14n#'> <ec:InclusiveNamespaces PrefixList='ds saml samlp xacml-context xacml-saml' xmlns:ec='http://www.w3.org/2001/10/xml-exc-c14n#'/> </ds:Transform> </ds:Transforms> <ds:DigestMethod Algorithm='http://www.w3.org/2000/09/xmldsig#sha1'/> <ds:DigestValue>uct4nBcdqAV4FIO50WMmFjSy9sE=</ds:DigestValue> </ds:Reference> </ds:SignedInfo> <ds:SignatureValue>dLaXFl6+HHqtaQoE8l22bCCM8byxblyBOYUTdUdG/LeYIR+ NUTn6nTRe9MJqWqrXT4qLtQ2Jvb3Cjrw66YZTdVrBXNjD1t6oWAg3YFXtZcO4s1+z5y4BeN6Mq spLLKIUnovCADNbHvhhVDwtMkCOcUs0x35R0zENiU1PYVMLQMM=</ds:SignatureValue> <ds:KeyInfo> <ds:X509Data> <ds:X509Certificate> MIICNTCCAZ6gAwIBAgIES343gjANBgkqhkiG9w0BAQUFADBVMQswCQYDVQQGEwJVUzELMAkGA1 UECAwCQ0ExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxDTALBgNVBAoMBFdTTzIxEjAQBgNVBAMM CWxvY2FsaG9zdDAeFw0xMDAyMTkwNzAyMjZaFw0zNTAyMTMwNzAyMjZaMFUxCzAJBgNVBAYTAl VTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzENMAsGA1UECgwEV1NPMjES MBAGA1UEAwwJbG9jYWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUp/oV1vWc8/ TkQSiAvTousMzOM4asB2iltr2QKozni5aVFu818MpOLZIr8LMnTzWllJvvaA5RAAdpbECb+48F jbBe0hseUdN5HpwvnH/DW8ZccGvk53I6Orq7hLCv1ZHtuOCokghz/ATrhyPq+QktMfXnRS4HrK GJTzxaCcU7OQIDAQABoxIwEDAOBgNVHQ8BAf8EBAMCBPAwDQYJKoZIhvcNAQEFBQADgYEAW5wP R7cr1LAdq+IrR44iQlRG5ITCZXY9hI0PygLP2rHANh+PYfTmxbuOnykNGyhM6FjFLbW2uZHQTY 1jMrPprjOrmyK5sjJRO4d1DeGHT/YnIjs9JogRKv4XHECwLtIVdAbIdWHEtVZJyMSktcyysFcv uhPQK8Qc/E/Wq8uHSCo= </ds:X509Certificate> </ds:X509Data> </ds:KeyInfo> </ds:Signature> <saml:Assertion IssueInstant='2011-10-31T06:49:51.008Z' Version='2.0' xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion'> <saml:Issuer SPProvidedID='SPPProvierId'>https://identity.carbon.wso2.org</saml:Issuer> <saml:Statement xmlns:xacml-saml='urn:oasis:names:tc:xacml:2.0:profile:saml2.0:v2:schema:assertion' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='xacml-saml:XACMLAuthzDecisionStatementType'> <xacml-context:Response xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'> <xacml-context:Result ResourceId='http://localhost:8280/services/echo/echoString' xmlns:xacml-context='urn:oasis:names:tc:xacml:2.0:context:schema:os'> <xacml-context:Decision>Permit</xacml-context:Decision> <xacml-context:Status><xacml-context:StatusCode Value='urn:oasis:names:tc:xacml:1.0:status:ok'/> </xacml-context:Status> </xacml-context:Result> </xacml-context:Response> </saml:Statement> </saml:Assertion> </samlp:Response> The XACML response is wrapped into a SAML statement which is included in a SAML assertion that is again wrapped by a SAML response.I have only signed the response according to the context and included only one assertion. We can separately sign both the assertion and response according to the spec and include more assertions in one response. Also it is possible to send the relevant XACML request inside the response and lot more options are there according to the spec. With OpenSAML we can get most of them into action. Reference: Implementing SAML to XACML from our JCG partner Pushpalanka at the Pushpalanka’s Blog blog....
spring-security-logo

REST + Spring Security session problem

REST, sessions .. wait. There are no sessions in REST application, right? Well, thats true. If we can avoid sessions we should do that. REST is stateless. The main concern about statelessness is authentication. In usual web applications we were used to store user data in session after authentication. How to solve that if we don’t want to use sessions? We should authenticate every request. Thanks to that we can scale our application, add new nodes, remove nodes without care about session replication and also about consumed Java heap memory. Recently I’ve been working on high load REST application. Actually we didn’t expect to have high traffic there but surprisingly we had much much higher that we have been prepared for (it’s so called “happy problem”). Application is based on Spring Framework and its secured with Spring Security deployed on Apache Tomcat 7. All resources are totally stateless – HttpSession is not touched by any piece of my code. Unfortunately used Java heap space was increasing all the time until: java.lang.OutOfMemoryError: Java heap space was thrown. In order to analyze Java heap dump and runtime usage of heap I used VisualVM.Heap dump analysis shown that big part of memory was used by ConcurrentHashMaps used by Tomcat to store sessions. Those sessions object were almost empty but there were so many of them that they consumed ~50% of reserved Java heap space.Add parameter to Tomcat startup script: -XX:+HeapDumpOnOutOfMemoryError to get Java heap dump on java.lang.OutOfMemoryError: Java heap space First thing that I’ve done was to limit session timeout from default 30 minutes to 1 minute (lowest possible option) in web.xml: <?xml version='1.0' encoding='UTF-8'?> <web-app xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd'version='3.0'> <!-- ... --><session-config> <session-timeout>1</session-timeout> </session-config> </web-app> That solved the problem – heap was cleaned by GC with better results and OutOfMemoryError was not thrown anymore. But more important thing is where those sessions were coming from? The answer is: Spring Security. By default Spring Security creates sessions if required – which means that if user has authenticated successfully then session is created. In my case it meaan – always. In order to prevent Spring Security from creating sessions create-session='never' needs to be added to http: <http create-session='never'> <!-- ... --> </http> You might think – empty session objects should not be a problem. I can tell you that for application handling few hundreds of requests per second it does a real change. Especially when its not running in the cloud or has few GB or RAM memory dedicated for Java heap. That’s how Java heap usage look like after those modifications:Reference: Solving REST + Spring Security session problem from our JCG partner Maciej Walkowiak at the Software Development Journey blog....
java-logo

How to analyze Thread Dump – Thread Stack Trace

This article is part 5 of our Thread Dump analysis series. So far you have learned the basic principles of Threads and their interactions with your Java EE container & JVM. You have also learned different Thread Dump formats for HotSpot and IBM Java VM’s. It is now time for you to deep dive into the analysis process. In order for you to quickly identify a problem pattern from a Thread Dump, you first need to understand how to read a Thread Stack Trace and how to get the “story” right. This means that if I ask you to tell me what the Thread #38 is doing; you should be able to precisely answer; including if Thread Stack Trace is showing a healthy (normal) vs. hang condition. Java Stack Trace revisited Most of you are familiar with Java stack traces. This is typical data that we find from server and application log files when a Java Exception is thrown. In this context, a Java stack trace is giving us the code execution path of the Thread that triggered the Java Exception such as a java.lang.NoClassDefFoundError, java.lang.NullPpointerException etc. Such code execution path allows us to see the different layers of code that ultimately lead to the Java Exception. Java stack traces must always be read from bottom-up:The line at the bottom will expose the originator of the request such as a Java / Java EE container Thread.  The first line at the top of the stack trace will show you the Java class where that last Exception got triggered.Let’s go through this process via a simple example. We created a sample Java program simply executing some Class methods calls and throwing an Exception. The program output generated is as per below: JavaStrackTraceSimulator Author: Pierre-Hugues Charbonneauhttp://javaeesupportpatterns.blogspot.comException in thread "main" java.lang.IllegalArgumentException: at org.ph.javaee.training.td.Class2.call(Class2.java:12) at org.ph.javaee.training.td.Class1.call(Class1.java:14) at org.ph.javaee.training.td.JavaSTSimulator.main(JavaSTSimulator.java:20)Java program JavaSTSimulator is invoked (via the “main” Thread)  The simulator then invokes method call() from Class1 Class1 method call() then invokes Class2 method call() Class2 method call() throws a Java Exception: java.lang.IllegalArgumentException  The Java Exception is then displayed in the log / standard outputAs you can see, the code execution path that lead to this Exception is always displayed from bottom-up. The above analysis process should be well known for any Java programmer. What you will see next is that the Thread Dump Thread stack trace analysis process is very similar to above Java stack trace analysis. Thread Dump: Thread Stack Trace analysis Thread Dump generated from the JVM provides you with a code level execution snapshot of all the “created” Threads of the entire JVM process. Created Threads does not mean that all these Threads are actually doing something. In a typical Thread Dump snapshot generated from a Java EE container JVM: Some Threads could be performing raw computing tasks such as XML parsing, IO / disk access etc.  Some Threads could be waiting for some blocking IO calls such as a remote Web Service call, a DB / JDBC query etc.   Some Threads could be involved in garbage collection at that time e.g. GC Threads  Some Threads will be waiting for some work to do (Threads not doing any work typically go in wait() state)  Some Threads could be waiting for some other Threads work to complete e.g. Threads waiting to acquire a monitor lock (synchronized block{}) on some objectsI will get back to the above with more diagrams in my next article but for now let’s focus on the stack trace analysis process. Your next task is to be able to read a Thread stack trace and understand what it is doing, on the best of your knowledge. A Thread stack trace provides you with a snapshot of its current execution. The first line typically includes native information of the Thread such as its name, state, address etc. The current execution stack trace has to be read from bottom-up. Please follow the analysis process below. The more experience you get with Thread Dump analysis, the faster you will able to read and identify very quickly the work performed by each Thread:Start to read the Thread stack trace from the bottom  First, identify the originator (Java EE container Thread, custom Thread ,GC Thread, JVM internal Thread, standalone Java program “main” Thread etc.)  The next step is to identify the type of request the Thread is executing (WebApp, Web Service, JMS, Remote EJB (RMI), internal Java EE container etc.)  The next step is to identify form the execution stack trace your application module(s) involved e.g. the actual core work the Thread is trying to perform. The complexity of analysis will depend of the layers of abstraction of your middleware environment and application  The next step is to look at the last ~10-20 lines prior to the first line. Identify the protocol or work the Thread is involved with e.g. HTTP call, Socket communication, JDBC or raw computing tasks such as disk access, class loading etc.  The next step is to look at the first line. The first line usually tells a LOT on the Thread state since it is the current piece of code executed at the time you took the snapshot  The combination of the last 2 steps is what will give you the core of information to conclude of what work and / or hanging condition the Thread is involved withNow find below a visual breakdown of the above steps using a real example of a Thread Dump Thread stack trace captured from a JBoss 5 production environment. In this example, many Threads were showing a similar problem pattern of excessive IO when creating new instances of JAX-WS Service instances.As you can see, the last 10 lines along with the first line will tell us what hanging or slow condition the Thread is involved with, if any. The lines from the bottom will give us detail of the originator and type of request. I hope this article has helped you understand the importance of proper Thread stack trace analysis. I will get back with much more Thread stack trace examples when we cover the most common Thread Dump problem patterns in future articles. The next article will now teach you how to breakdown the Thread Dump threads in logical silos and come up with a potential list of root cause “suspects”. Reference: How to analyze Thread Dump – Part 5: Thread Stack Trace from our JCG partner Pierre-Hugues Charbonneau at the Java EE Support Patterns & Java Tutorial blog....
git-logo

Mobicents Diameter has a new home.. powered by git!

Due to the increasingly number and complexity of sub-projects, Mobicents has been splitting it’s projects under independent project homes. We are now at a transition stage, where each project lead will migrate his project at the appropriate time. For Diameter, after our major release (1.4.0.FINAL), it seemed the best time to do this migration. The new home of the Mobicents Diameter project is located at http://code.google.com/p/jdiameter/. Feel free to visit us! With this change, we have also taken the chance to move to a “better” and more powerful version control system, so we have changed from SVN to Git. We hope this will ease the contributions, which have been happening more often, thanks to out thriving community! Despite being a allegedly better VCS, Git also has it’s own shortcomings, such as not being able to checkout a single folder. The lack of this feature has impacted our structure, since it makes it impossible to independently release sub-components in the same Git repository. While digging for a solution, I’ve came across the “sparse checkout” feature. While this may work for checking out only some folder(s), it is not supported by maven release plugin, thus not fixing the main problem. Another possible (and probably more correct) solution would be to use the “sub-repository” approach, where we’d split the components into different sub-repositories. We actually did this on a first phase, but after realizing the big changes it would imply and being aware that google code messes with the automatic revision links ( http://code.google.com/p/support/issues/detail?id=3245) we have decided to revert back to the single repository approach and abdicate from the release independency for each component. We have barely used it, anyway. The only exception is for the Jopr RHQ Plugin, so it got it’s own sub-repository. As a reference for someone going through the same process of moving from a [googlecode] SVN repository to a [googlecode] git repository, I’ll leave a summary of the commands used for the move (please keep in mind that I’m totally new to git, so these may not be the optimal way, feel free to comment): 0. (optional) Since SVN only lists the username and git uses the email as well, the migration will cause weird authors such as “brainslog <brainslog@bf0df8d0-2c1f-0410-b170-bd30377b63dc>” it may be good to sanitize the authors. For that I’ve used the following script in the SVN repository: svn log -q | awk -F '|' '/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" <"$2">"}' | sort -u > authors.txtAnd then I’ve fixed the outcome in authors.txt to the correct: username = Full Name . 1. Clone the new (and probably empty) repository git clone https://@code.google.com/p/jdiameter/ git-jdiameter2. Clone from SVN (with authors fixed) to the cloned git repository, with full history, tags and branches git svn clone -A authors.txt -Ttrunk/servers/diameter -ttags/servers/diameter -bbranches/servers/diameter https://mobicents.googlecode.com/svn git-jdiameterWhere -A points to the authors file, -T to the trunk of the SVN repository, -t to the tags and -b to the branches. Use only what you need, I didn’t needed the branches part as we don’t have any. 3. Enter the local git repository cd git-jdiameter4. Push! git push --all5. Create the git tags from SVN tags (I know this can be automatized but I needed to make some customizations to existing tags, as not all were correct) git tag "1.0.0.BETA1" "refs/remotes/tags/jdiameter-1.0.0.BETA1" ...git tag "1.0.0.FINAL" "refs/remotes/tags/1.0.0.FINAL"6. Push.. again! git push --tagsThese are the 7 magical steps! In case that you don’t care about history, simply do a svn export --force http://mobicents.googlecode.com/svn/trunk/servers/diameter/ git-jdiameterand push it, nothing else. Also, as mentioned, I did not had branches but I suppose they’d make it in the #4 push. Mobicents Diameter users working with trunk master, must now switch to this new repository as the SVN will not be updated anymore! Do a ” git clone https://code.google.com/p/jdiameter/” and hack away! Reference: Mobicents Diameter has a new home.. powered by git! from our JCG partner Alexandre Mendonca at the Alexandre Mendonca blog blog....
apache-thrift-logo

Apache Thrift with Java quickstart

Apache Thrift is a RPC framework founded by facebook and now it is an Apache project. Thrift lets you define data types and service interfaces in a language neutral definition file. That definition file is used as the input for the compiler to generate code for building RPC clients and servers that communicate over different programming languages. You can refer Thrift white paper also. According to the official web site Apache Thrift is a, software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages. Image courtesy wikipediaInstalling Apache Thrift in Windows Installation Thrift can be a tiresome process. But for windows the compiler is available as a prebuilt exe. Download thrift.exe and add it into your environment variables. Writing Thrift definition file (.thrift file) Writing the Thrift definition file becomes really easy once you get used to it. I found this tutorial quite useful to begin with. Example definition file (add.thrift) namespace java com.eviac.blog.samples.thrift.server // defines the namespacetypedef i32 int //typedefs to get convenient names for your typesservice AdditionService { // defines the service to add two numbers int add(1:int n1, 2:int n2), //defines a method }Compiling Thrift definition file To compile the .thrift file use the following command. thrift --gen <language> <Thrift filename>For my example the command is, thrift --gen java add.thriftAfter performing the command, inside gen-java directory you’ll find the source codes which is useful for building RPC clients and server. In my example it will create a java code called AdditionService.java Writing a service handler Service handler class is required to implement the AdditionService.Iface interface. Example service handler (AdditionServiceHandler.java) package com.eviac.blog.samples.thrift.server;import org.apache.thrift.TException;public class AdditionServiceHandler implements AdditionService.Iface {@Override public int add(int n1, int n2) throws TException { return n1 + n2; }}  Writing a simple server Following is an example code to initiate a simple thrift server. To enable the multithreaded server uncomment the commented parts of the example code. Example server (MyServer.java) package com.eviac.blog.samples.thrift.server;import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServer.Args; import org.apache.thrift.server.TSimpleServer;public class MyServer {public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) { try { TServerTransport serverTransport = new TServerSocket(9090); TServer server = new TSimpleServer( new Args(serverTransport).processor(processor));// Use this for a multithreaded server // TServer server = new TThreadPoolServer(new // TThreadPoolServer.Args(serverTransport).processor(processor));System.out.println("Starting the simple server..."); server.serve(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler())); }}Writing the client Following is an example java client code which consumes the service provided by AdditionService. Example client code (AdditionClient.java) package com.eviac.blog.samples.thrift.client;import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException;public class AdditionClient {public static void main(String[] args) {try { TTransport transport;transport = new TSocket("localhost", 9090); transport.open();TProtocol protocol = new TBinaryProtocol(transport); AdditionService.Client client = new AdditionService.Client(protocol);System.out.println(client.add(100, 200));transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException x) { x.printStackTrace(); } }}Run the server code(MyServer.java). It should output following and will listen to the requests. Starting the simple server...Then run the client code(AdditionClient.java). It should output following. 300Reference: Apache Thrift with Java quickstart from our JCG partner Pavithra Siriwardena at the EVIAC blog....
jsf-logo

Four solutions to the LazyInitializationException – Part 1

In the post today we will talk about the common LazyInitializationException error. We will see four ways to avoid this error, the advantage and disadvantage of each approach and in the end of this post, we will talk about how the EclipseLink handles this exception. To see the LazyInitializationException error and to handle it, we will use an application JSF 2 with EJB 3. Topics of the post:Understanding the problem, Why does LazyInitializationException happen? Load collection by annotation Load collection by Open Session in View (Transaction in View) Load collection by Stateful EJB with PersistenceContextType.EXTENDED Load collection by Join Query EclipseLink and lazy collection initializationAt the end of this post you will find the source code to download. Attention: In this post we will find an easy code to read that does not apply design patterns. This post focus is to show solutions to the LazyInitializationException. The solutions that you will find here works for web technology like JSP with Struts, JSP with VRaptor, JSP with Servlet, JSF with anything else. Model Classes In the post today we will use the class Person and Dog: package com.model;import javax.persistence.*;@Entity public class Dog {@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name;public Dog() {}public Dog(String name) { this.name = name; }//get and set } package com.model;import java.util.*;import javax.persistence.*;@Entity public class Person {@Id @GeneratedValue(strategy = GenerationType.AUTO) private int id;private String name;@OneToMany @JoinTable(name = 'person_has_lazy_dogs') private List<Dog> lazyDogs;public Person() {}public Person(String name) { this.name = name; }// get and set } Notice that with this two classes we will be able to create the LazyInitializationException. We have a class Person with a Dog list. We also will use a class to handle the database actions (EJB DAO) and a ManagedBean to help us to create the error and to handle it: package com.ejb;import java.util.List;import javax.ejb.*; import javax.persistence.*;import com.model.*;@Stateless public class SystemDAO {@PersistenceContext(unitName = 'LazyPU') private EntityManager entityManager;private void saveDogs(List<Dog> dogs) { for (Dog dog : dogs) { entityManager.persist(dog); } }public void savePerson(Person person) { saveDogs(person.getLazyDogs()); saveDogs(person.getEagerDogs()); entityManager.persist(person); }// you could use the entityManager.find() method also public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } } package com.mb;import javax.ejb.EJB; import javax.faces.bean.*;import com.ejb.SystemDAO; import com.model.*;@ManagedBean @RequestScoped public class DataMB {@EJB private SystemDAO systemDAO;private Person person;public Person getPerson() { return systemDAO.findByName('Mark M.'); } } Why does LazyInitializationException happen? The Person class has a Dog list. The easier and fattest way to display a person data would be, to use the entityManager.find() method and iterate over the collection in the page (xhtml). All that we want was that the code bellow would do this… // you could use the entityManager.find() method also public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } public Person getPerson() { return systemDAO.findByName('Mark M.'); } <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml' xmlns:f='http://java.sun.com/jsf/core' xmlns:h='http://java.sun.com/jsf/html' xmlns:ui='http://java.sun.com/jsf/facelets'> <h:head></h:head> <h:body> <h:form> <h:dataTable var='dog' value='#{dataMB.personByQuery.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> </h:form> </h:body> </html> Notice that in the code above, all we want to do is to find a person in the database and display its dogs to an user. If you try to access the page with the code above you will see the exception bellow: [javax.enterprise.resource.webcontainer.jsf.application] (http–127.0.0.1-8080-2) Error Rendering View[/getLazyException.xhtml]: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.model.Person.lazyDogs, no session or session was closedat org.hibernate.collection.internal.AbstractPersistentCollection. throwLazyInitializationException(AbstractPersistentCollection.java:393) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]at org.hibernate.collection.internal.AbstractPersistentCollection. throwLazyInitializationExceptionIfNotConnected (AbstractPersistentCollection.java:385) [ hibernate-core-4.0.1.Final.jar:4.0.1.Final]at org.hibernate.collection.internal.AbstractPersistentCollection. readSize(AbstractPersistentCollection.java:125) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]To understand better this error let us see how the JPA/Hibernate handles the relationship. Every time we do a query in the database the JPA will bring to all information of that class. The exception to this rule is when we talk about list (collection). Image that we have an announcement object with a list of 70,000 of emails that will receive this announcement. If you want just to display the announcement name to the user in the screen, imagine the work that the JPA would have if the 70,000 emails were loaded with the name. The JPA created a technology named Lazy Loading to the classes attributes. We could define Lazy Loading by: “the desired information will be loaded (from database) only when it is needed”. Notice in the above code, that the database query will return a Person object. When you access the lazyDogs collection, the container will notice that the lazyDogs collection is a lazy attribute and it will “ask” the JPA to load this collection from the database. In the moment of the query (that will bring the lazyDogs collection) execution, an exception will happen. When the JPA/Hibernate tries to access the database to get this lazy information, the JPA will notice that there is no opened collection. That is why the exception happens, the lack of an opened database connection. Every relationship that finishes with @Many will be lazy loaded by default: @OneToMany and @ManyToMany. Every relationship that finishes with @One will be eagerly loaded by default: @ManyToOne and @OneToOne. If you want to set a basic field (E.g. String name) with lazy loading just do: @Basic(fetch=FetchType.LAZY). Every basic field (E.g. String, int, double) that we can find inside a class will be eagerly loaded if the developer do not set it as lazy. A curious subject about default values is that you may find each JPA implementation (EclipseLink, Hibernate, OpenJPA) with a different behavior for the same annotation. We will talk about this later on. Load collection by annotation The easier and the fattest way to bring a lazy list when the object is loaded is by annotation. But this will not be the best approach always. In the code bellow we will se how to eagerly load a collection by annotation: @OneToMany(fetch = FetchType.EAGER) @JoinTable(name = 'person_has_eager_dogs') private List<Dog> eagerDogs; <h:dataTable var='dog' value='#{dataMB.person.eagerDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsEasy to set upIf the class has several collections this will not be good to the server performanceThe list will always come with the loaded objectIf you want to display only a basic class attribute like name or age, all collections configured as EAGER will be loaded with the name and the ageThis approach will be a good alternative if the EAGER collection have only a few items. If the Person will only have 2, 3 dogs your system will be able to handle it very easily. If later the Persons dogs collection starts do grow a lot, it will not be good to the server performance. This approach can be applied to JSE and JEE. Load collection by Open Session in View (Transaction in View) Open Session in View (or Transaction in View) is a design pattern that you will leave a database connection opened until the end of the user request. When the application access a lazy collection the Hibernate/JPA will do a database query without a problem, no exception will be threw. This design pattern, when applied to web applications, uses a class that implements a Filter, this class will receive all the user requests. This design pattern is very easy to apply and there is two basic actions: open the database connection and close the database connection. You will need to edit the “web.xml” and add the filter configurations. Check bellow how our code will look like: <filter> <filter-name>ConnectionFilter</filter-name> <filter-class>com.filter.ConnectionFilter</filter-class> </filter> <filter-mapping> <filter-name>ConnectionFilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> package com.filter;import java.io.IOException;import javax.annotation.Resource; import javax.servlet.*; import javax.transaction.UserTransaction;public class ConnectionFilter implements Filter {@Override public void destroy() {}@Resource private UserTransaction utx;@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { utx.begin(); chain.doFilter(request, response); utx.commit(); } catch (Exception e) { e.printStackTrace(); }}@Override public void init(FilterConfig arg0) throws ServletException {} } <h:dataTable var='dog' value='#{dataMB.person.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsThe model classes will not need to be editedAll transaction must be handled in the filter classThe developer must be very cautious with database transaction errors. A success message can be sent by the ManagedBean/Servlet, but when the database commits the transacion an error may happenN+1 effect may happen (more detail bellow)The major issue of this approach is the N+1 effect. When the method returns a person to the user page, the page will iterate over the dogs collection. When the page access the lazy collection a new database query will be fired to bring the dog lazy list. Imagine if the Dog has a collection of dogs, the dogs children. To load the dogs children list other database query would be fired. But if the children has other children, again the JPA would fire a new database query… and there it goes… This is the major issue of this approach. A query can create almost a infinity number of other queries. This approach can be applied to JSE and JEE. Continue to the second part of this tutorial. Reference: Four solutions to the LazyInitializationException from our JCG partner Hebert Coelho at the uaiHebert blog....
jsf-logo

Four solutions to the LazyInitializationException – Part 2

This article continues from part 1 of the tutorial. Load collection by Stateful EJB with PersistenceContextType.EXTENDED This approach can be applied only to applications that works with Full JEE environments: to use a EJB with PersistenceContextType.EXTENDED. Check the code below how the DAO would look like: package com.ejb;import javax.ejb.Stateful; import javax.persistence.*;import com.model.Person;@Stateful public class SystemDAOStateful { @PersistenceContext(unitName = 'LazyPU', type=PersistenceContextType.EXTENDED) private EntityManager entityManager;public Person findByName(String name) { Query query = entityManager.createQuery('select p from Person p where name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } } public class DataMB { // other methods and attributes@EJB private SystemDAOStateful daoStateful;public Person getPersonByStatefulEJB() { return daoStateful.findByName('Mark M.'); } } <h:dataTable var='dog' value='#{dataMB.personByStatefulEJB.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsThe container will control the database transactionWorks only for JEEThe model classes will not need to be editedN+1 effect may happenA great amount of Stateful EJBs may affect the container memory.This approach may create the N+1 effect and the Stateful EJB has a characteristic of not being removed/destroyed while the its session is not expired or until it lost its reference. Warning: It is not a good practice to hold a reference to an injected EJB in objects that remain in a Pool. The JSF will create a ManagedBean pool to handle better the user requests; when it is necessary the container will increase or decrease the number of ManagedBeans in the pool. In the code of this post imagine if the container create 100 instances of ManagedBeans in the pool, the server will hold 100 Stateful EJBs in memory. The solution to this problem would be a JNDI LookUp to the Stateful EJB. Load collection by Join Query This solution is easy to understand and to apply. See the code below: public Person findByNameWithJoinFech(String name) { Query query = entityManager.createQuery('select p from Person p join fetch p.lazyDogs where p.name = :name'); query.setParameter('name', name);Person result = null; try { result = (Person) query.getSingleResult(); } catch (NoResultException e) { // no result found }return result; } public Person getPersonByQuery() { return systemDAO.findByNameWithJoinFech('Mark M.'); } <h:dataTable var='dog' value='#{dataMB.personByQuery.lazyDogs}'> <h:column> <f:facet name='header'> Dog name </f:facet> #{dog.name} </h:column> </h:dataTable> Pros and Cons of this approach:ProsConsJust one query will be fired in the databaseIt would be necessary one query for each accessed collection/lazy attributeThe model classes will not need to be editedWill bring only the desired dataThe N+1 effect will not happenThis approach has the disadvantage of the need of a new query to access each model class collection/lazy attribute. If we need to query only the Person dogs we would need of a specific query. Imagine that we would need to query for the Person emails, it would be necessary a different query. This approach can be applied to JSE and JEE. EclipseLink and lazy collection initialization The relationships default values are:RelationshipFetch@OneToOneEAGER@OneToManyLAZY@ManyToOneEAGER@ManyToManyLAZYBut the JPA Spec* says that: The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed. The implementation is permitted to eagerly fetch data for which the LAZY strategy hint has been specified. In particular, lazy fetching might only be available for Basic mappings for which property-based access is used. As you can see in the text above, the JPA implementation may ignore the hint strategy if it wants to. The EclipseLink has a behavior with JEE and other behavior to JSE. You can see each behavior here: http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#What_You_May_Need_to_Know_About_EclipseLink_JPA_Lazy_Loading We can find in the internet some people saying that even with a lazy collection the EclipseLink does the n+1 queries when the entity is loaded. And we can find this behavior of users with Glassfish and EJB. Below you will see some tips to use correctly lazy load with EclipseLink:http://stackoverflow.com/questions/8490532/eclipselink-lazy-loading http://stackoverflow.com/questions/3932623/eclipselink-dont-fetch-some-fields-by-default https://forums.oracle.com/forums/thread.jspa?messageID=1706796* JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence) 9.1.18 and will repeat to the otters JPA relationships. The end! In my opinion the best solution is the Join Fetch Query. It is up to you to choose the best solution to your application. Click here to download the source code of this post. If you want to run the code of this post you will need to create a database named LazyExceptionDB and the JBoss module. Attached to the source code is the Postgres module. If you want to see how to set up the datasource and the Postgres or MySQL module you can see it here: Full WebApplication JSF EJB JPA JAAS. I hope this post might help you. If you have any comment or doubt just post it. See you soon. Reference: Four solutions to the LazyInitializationException from our JCG partner Hebert Coelho at the uaiHebert blog....
agile-logo

Architects Need a Pragmatic Software Development Process

I have been a non-stop software architect since 2006. During my experience, I realized that it’s really hard to perform the role of architect in an organization that doesn’t have a software development process or have it too simplified. When the development is not fairly organized, project managers don’t find a room in their schedule to implement architectural recommendations. They probably have time and resources, but since they don’t have a precise idea of the team’s productivity, they feel afraid of accepting new non-functional requirements or changing existing ones. I’ve noticed that, in a chaotic environment, people become excessively pragmatic, averse to changes. Architects expect that the organization adopts a more predictable and transparent software process. This way, it’s possible to visualize the impact of recommendations and negotiate when they are going to be implemented. They minimally expect a process that has iterations inspired on the classical PDCA (Plan, Do, Check and Act) cycle because it has loops with feedback, which are the foundation for continuous improvement. The figure below depicts what could be considered as a pragmatic software process.Iterations are overlapped in time in order to optimize the use of resources and guarantee the feedback from previous iterations. Each iteration is performed in a fixed period of time. This time depends on the context and it tends to fall as the organization gains more maturity. An iteration is composed of 4 phases (plan, do, check and act) and 5 events that may occur according to the planning. They are:T1: It represents the beginning of the iteration, starting with its planning. The scope of the planning covers only the period of the current iteration. It should not be mixed with the general project planning, which is produced in one of the initial iterations to plan all other iterations. All members of the team participate in the planning. T2: The execution of what was planned for the iteration starts. All members of the team must have something to do within the scope of the iteration. Nothing planned for future iterations should be done in the current iteration. People may produce all sort of output, such as documents, code, reports, meeting minutes, etc. T3: Everything that is produced should be checked. Documents should be reviewed, code should be tested, user interfaces and integrations with other systems should be tested, etc. All found issues must be registered to be solved in due time. T4: Solve all issues found during the check phase and release all planned deliverables. Everybody should deliver something. In case time is not enough to solve some found issues, they must be included in the planning of the next iteration with the highest priority. Statistics should be produced during this phase in order to compare the planning with the execution. The planning of the next iteration also starts at this point, taking advantage of the experience from the previous iteration. T5: Once everything is released, the current iteration finishes. T2 of the next iteration immediately starts because most of resources are already available.T1 to T5 repeats several times, in a fixed period of time, until the end of the project. This suggestion is process-agnostic, thus it can be implemented no matter what software process we claim to have in place or any other modern process we can think of. In addition to the process, there are also some good practices:Consider everything that describes how the system implements business needs as use cases. It can also be functional and nonfunctional requirements, user stories, scenarios, etc; but there must be only one sort of artefact to describe business needs. Write use cases in a way that the text can be reused to: a) help business people to visualize how their needs will work; b) guide testers on the exploratory tests; and c) help the support team to prepare the user manual. Avoid technical terms in use cases. If really needed, technical details may be documented in another artefact, such as use case realizations. If needed, create use case realizations using UML models only. Representing use case realisations as documents implies on a huge overhead. Any necessary textual information can be added in the comments area of UML’s elements. Fix the size of use cases according to the effort to realize it. For example: we can fix that the maximum size of a use case is 1 week. If the estimation is higher than that, then the use case must be divided in two others. If the estimation is far lower than that, then the use case must be merged with another closely related use case. By simply counting the number of use cases we immediately know the effort and the resources required to execute the project. This fixed number is a parameter to compare the planning with the execution. By performing this comparison after every iteration, we gradually know how precise our estimations are becoming. Use a wiki to document use cases and other required documentations, such as test cases, release notes, etc. Create a wiki page for each use case and use signs to indicate what is still pending to be released. The advantages of the wiki are: a) use cases are immediately available for all stakeholders as they are gathered; b) stakeholders can follow the evolution of the use cases by following updates in the page; c) it’s possible to know everyone who contributed to the use case and what exactly they did; and d) it’s possible to add comments to use cases, preserving all the discussion around it. If the organization has business processes, which is another thing that architects also love, then put references in the business process’ activities pointing to the use cases that implement them. A reference is a link to the page where the use case is published on the wiki. Follow up use cases using an issue tracking system, such as Jira. Each use case must have a corresponding Jira ticket and every detail of the use case’s planning, execution, checking and delivery must be registered in that ticket. The advantages of linking Jira tickets with use cases are: a) Jira tickets represent the execution of the planning and their figures can be compared with the planning, generating statistics on which managers can rely on; b) we know exactly every person who contributed to the use case, what they did, and for how long; and c) it’s an important source of lessons learned. Test, test, test! It must be an obsessive compulsive behaviour. Nothing goes out without passing through the extensive test session. Constantly train and provide all needed bibliography to the team on the technologies in use. The more technical knowledge we have inside of the team, the highest is our capability to solve problems and increase productivity.Working this way, everything becomes quantifiable, predictable, comparable and traceable. From the practices above we can extract the traceability flow from business to the lowest IT level, as depicted in the figure below.Business process elements such as swimlanes and activities may inspire actors and use cases. Use cases and actors are documented on wiki pages. Each use case and actor has a page on the wiki, which has a unique URL and can be used to refer the element on email messages, documents, Jira tickets and so on. An Jira ticket is created for each use case and it contains a link to the use case’s wiki page. This wiki page can also have a link to the ticket since it also has a unique URL. Jira tickets can be automatically linked the source code through the version control system (SVN) and declaratively linked to system’s features and user interfaces. Since it’s possible to create mock-ups in wiki pages, then we also link those wiki pages with user interfaces to compare the mock-ups with the final user interface. We finally have actors linked to security roles. I admit that architects are not qualified to define and implement a software development process in the organization (they actually believe more in the Programming Motherfucker philosophy :D), but they are constantly willing to contribute to have one in place. As they have instruments to monitor servers, releases, tests, performance and so on, they also want project managers having instruments to estimate effort, predict events, anticipate problems and, therefore, produce better planning and results. Warning: Whatever we put in our software processes that is not quantifiable or measurable will become an expensive overhead. Reference: Architects Need a Pragmatic Software Development Process from our JCG partner Hildeberto Mendonca at the Hildeberto’s Blog blog....
javafx-logo

JavaFX 2.0 Layout Panes – HBox and VBox

If you want an overview on all different layout panes in JavaFX 2.0 or if you want to know yome basic facts about them, please see my previous post Layout Panes in JavaFX 2.0.The layout panes HBox and VBox are definitely the most basic layout containers in JavaFX 2.0. As you can already tell by their name, their purpose is to layout all their children in one horizontal row (HBox) or in one vertical column (VBox). Because they´re very easy to use and very useful regarding minor layout issues, you´ll probably use them a lot. I´ll give two examples on how you can use them. As in the other examples, first of all the code and afterwards the explanations. HBox and VBox – Example 1   import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;/** * * Created on: 20.03.2012 * @author Sebastian Damm */ public class HBoxandVBoxExample extends Application { @Override public void start(Stage primaryStage) throws Exception { HBox hbox = new HBox(50); hbox.setAlignment(Pos.CENTER); // default TOP_LEFT VBox vbox1 = new VBox(); vbox1.setAlignment(Pos.BOTTOM_CENTER); vbox1.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); VBox vbox2 = new VBox(10); vbox2.setAlignment(Pos.CENTER); vbox2.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); VBox vbox3 = new VBox(20); vbox3.setAlignment(Pos.TOP_CENTER); vbox3.setStyle("-fx-border-style: solid;" + "-fx-border-width: 1;" + "-fx-border-color: black"); for (int i = 0; i < 5; i++) { Button bt = new Button("Button " + (i+1)); Button bt2 = new Button("Button " + (i+1)); // unfortunately there´s no "clone" or "copy" method Button bt3 = new Button("Button " + (i+1));vbox1.getChildren().add(bt); vbox2.getChildren().add(bt2); vbox3.getChildren().add(bt3); } hbox.getChildren().addAll(vbox1, vbox2, vbox3); Scene scene = new Scene(hbox, 350, 250); // the hbox is the root node primaryStage.setTitle("HBox and VBox Example"); primaryStage.setScene(scene); primaryStage.show(); }public static void main(String[] args) { Application.launch(args); } } Basically we create three different VBoxes and put them into one HBox. In both classes you can define a spacing value either directly in the constructor or via the setSpacing method. This value will be used as the gap between the individual children in the pane. The line HBox hbox = new HBox(50); therefore creates a HBox to hold three VBoxes with a gap of 50 pixel between each of them.We use the setAlignment method to specify, how the individual VBoxes should arrange and layout all their childen.With setStyle you can apply custom CSS styles to any Node. I don´t want to go into much detail yet, because I´ll cover CSS styles in JavaFX 2.0 in one of my next posts, but if you´re already familiar with CSS you´ll probably already have noticed that the JavaFX 2.0 team fortunately decided to follow the CSS standards defined by W3C (http://www.w3.org) very closely. If you´re not familiar with CSS you just need to know that theses lines of CSS create a 1px wide black border around the component. I use them here to show you the size of the individual VBoxes.The next few lines should be pretty ordinary for you by now: We create five buttons for each VBox, put the different VBoxes into our HBox, declare a Scene object (with the HBox as root) and show our application.  Your application should look like this now:You can see that each VBox lays out their children buttons with the defined spacing value and that the HBox lays out the three VBoxes with a gap of 50 pixels. Because we set a different alignment for each VBox you can see that that they arrange the buttons inside their bounds in a specific way. Note: We also specified a alignment for the HBox, otherwise the three VBoxes would not have been layed out in the center! If you resize the window of your application it should look like this:You can see that the VBoxes grow and fill the space provided by their parent and that they still arrange their children according to their set alignment.HBox and VBox – Example 2The next example will show how to use the static setMargin and setHgrow (respectively setVgrow) methods: import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage;/** * * Created on: 20.03.2012 * @author Sebastian Damm */ public class HBoxandVBoxExample2 extends Application { @Override public void start(Stage primaryStage) throws Exception { StackPane root = new StackPane(); HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes // create a vbox with a textarea that grows vertically VBox vbox = new VBox(10); Label lbName = new Label("I´m a label!"); TextField textField = new TextField(); TextArea textArea = new TextArea(); textArea.setPrefWidth(100); VBox.setVgrow(textArea, Priority.ALWAYS); vbox.getChildren().addAll(lbName, textField, textArea); // create a vbox that grows horizontally inside the hbox VBox vbox2 = new VBox(10); Label lbName2 = new Label("I´m also a label!"); TextField tf2 = new TextField(); tf2.setPromptText("type here"); TextArea textArea2 = new TextArea(); textArea2.setPrefWidth(100); vbox2.getChildren().addAll(lbName2, tf2, textArea2); HBox.setHgrow(vbox2, Priority.ALWAYS);// the next two lines behave equally - try to comment the first line out and use the 2nd line hbox.setPadding(new Insets(20)); // StackPane.setMargin(hbox, new Insets(20));hbox.getChildren().addAll(vbox, vbox2); root.getChildren().add(hbox); Scene scene = new Scene(root, 500, 300); // the stack pane is the root node primaryStage.setTitle("HBox and VBox Example 2"); primaryStage.setScene(scene); primaryStage.show(); }public static void main(String[] args) { Application.launch(args); } } In this example we create two VBoxes that are children of a HBox. Inside the VBoxes there are one label, one textfield and one textarea.The first remarkable line is VBox.setVgrow(textArea, Priority.ALWAYS). With this line we define, that the TextArea object should always grow vertically if it is contained by a VBox (otherwise there won´t be any effects). Next, put a focus on HBox.setHgrow(vbox2, Priority.ALWAYS). Here we tell the second VBox to grow horizontally inside the HBox.Finally with hbox.setPadding(new Insets(20)); or StackPane.setMargin(hbox, new Insets(20)) we give the whole HBox some padding. The two lines behave equally here because on the one side we specify a padding for the pane itself that should be used by the pane inside its bounds to layout its children and on the other side we tell the parent to lay out the pane with the given margin around it.Note: Margin is the outer distance/gap, padding the inner one. Here is a picture showing you the ‘CSS Box Model’ that demonstrates the relation between the content, the padding, the border, and the margin of an element.Source: w3Schools.comBecause the setPadding method is defined in the Region class, every layout pane can use this method. The Insets class is used a lot for those purposes and provides two different constructors: One that takes one double value and defines the same padding for each side and one constructor that takes four double values and defines the padding clockwise from the top to the left side.Hint: If you apply a border to the HBox and switch between the two lines for setting padding/margin, you will see the difference more clearly.Your application should look like this:Note the 10px padding around the HBox. If you now resize you window it should look similar to this:As you can see the TextArea in the left VBox grows vertically and the whole right VBox grows horizontally inside the HBox. Reference: JavaFX 2.0 Layout Panes – HBox and VBox from our JCG partner Sebastian Damm at the Just my 2 cents about Java blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close