tag:blogger.com,1999:blog-51607886276036203902024-03-18T20:24:18.925-07:00BeanBelt...Where Beans thriveAnonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5160788627603620390.post-92211580544312778622014-07-03T11:12:00.001-07:002014-07-03T11:12:24.514-07:00Running GlassFish v3.1.2.2 on JDK 8GlassFish v3 will simply not run on JDK 8 in the standard configuration but I found a little tweak to accomplish this.<br />
<br />
Open the file <pre class="brush: bash">${GLASSFISH_HOME}/glassfish/config/osgi.properties</pre>and add this line at the end:<br />
<pre class="brush: bash">jre-1.8=${jre-1.7}
</pre><br />
<br />
Happy Coding!Anonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.com1tag:blogger.com,1999:blog-5160788627603620390.post-24229602178755181892014-04-24T12:31:00.002-07:002014-04-30T13:36:16.765-07:00JDBC: Connection could not be allocated because:<blockquote>Connection could not be allocated because: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Please check the server.log for more details. </blockquote><p>I stumbled about this Exception and wanted to let you know how I solved it for me. In my case that Exception comes from a GlassFish v4 instance interacting with a MySQL v5.5.37 database over JDBC v5.1.26. GlassFish wants to tell us that he can't receive any packages from the database but doesn't tell us exactly what's the reason. I figured out that the default value for the JDBC idle timeout are 300 secs but for MySQL the default value are (in my case) 180 secs. In this scenario MySQL is the server and GlassFish the client. The client connects to the server and keeps a connection. After 180 secs the server may kills the connection due to inactivity. The client does not get any information about this and keeps the connection for further 120 secs. If you call for data within these 120 secs you probably run into that exception because the connection is already closed and the client (GlassFish) complains with that exception.<br />
</p><p>So the <b>solution</b> is to lower the <b>idle timeout</b> in <b>GlassFish</b> to a value <b>lower than 180 secs</b> <b>or</b> raise the <b>wait_timeout</b> value in <b>MySQL</b> to a value <b>bigger than 300 secs</b>.<br />
</p><p>To change the idle timeout in GlassFish:<br />
- Go to http://{ip of server or localhost}:4848/<br />
- Go to <code>Resources -> JDBC -> JDBC Connection Pools</code> and select your pool<br />
- Scroll down to <code>Pool Settings</code> and change the value of <code>Idle Timeout:</code><br />
- Hit <code>Save</code><br />
</p><p>To change the wait_timeout in MySQL:<br />
- Change the dir to <code>/etc/mysql/</code><br />
- Open the <code>my.cnf</code> file and search for a entry called <code>wait_timeout</code><br />
- Change the value and save the document (May you have to restart MySQL too).<br />
</p>Hope this helps and...<br />
<br />
<br />
Happy Coding!Anonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.com3tag:blogger.com,1999:blog-5160788627603620390.post-88487980500060076092014-04-24T06:04:00.001-07:002014-04-30T13:37:50.542-07:00Introducing TabSwitch - A NetBeans IDE Plugin for efficiently switching Editor Tabs<ul><li><a href="https://github.com/Yserz/TabSwitch">Sourcecode on GitHub</a></li>
<li><a href="http://plugins.netbeans.org/plugin/54634/?show=true">NetBeans Plugin Center</a></li>
<li><a href="https://bitbucket.org/api/1.0/repositories/Yserz/ownmavenrepo/raw/HEAD/de/yser/TabSwitch/0.1/TabSwitch-0.1.nbm" download>Download from Repository</a></li>
</ul><br />
<p><b>I'm proud to announce my first NetBeans IDE Plugin called TabSwitch!</b><br />
TabSwitch is a Plugin for efficiently switching Editor Tabs in NetBeans IDE.I'm using multiple tabs heavily in my Browser and I'am used to switch them with shortcuts like <br />
</p><div class="separator" style="clear: both; text-align: center;"><code>⌥ + ⌘ + ←</code><br />
and<br />
<code>⌥ + ⌘ + ➝</code><br />
on Mac OS X or<br />
<br />
<code>Alt + Ctrl + ←</code><br />
and<br />
<code>Alt + Ctrl + ➝</code><br />
on Windows.<br />
</div><p>So I visited NetBeans Platform Training in Leipzig with <a href="https://blogs.oracle.com/geertjan/">Geertjan Wielanga</a>, <a href="http://eppleton.de/">Anton Eppleton</a> and <a href="http://benkiew.wordpress.com/">Benno Markiewicz</a> and wrote TabSwitch to use this feature in NetBeans IDE too.To be honest, NetBeans IDE already provides a similar feature which you can use with<br />
</p><br />
<div class="separator" style="clear: both; text-align: center;"><code>⌘ + PAGE_UP</code><br />
and<br />
<code>⌘ + PAGE_DOWN</code><br />
on Mac OS X or<br />
<br />
<code>Ctrl + PAGE_UP</code><br />
and<br />
<code>Ctrl + PAGE_DOWN</code><br />
on Windows.<br />
</div><p>But if you work with that built-in feature for a while, you will notice, that it will switch between project tabs unexpectedly if you use the "tab-row-per-project" feature of NetBeans IDE. Which made this feature unuseable for me. With TabSwitch that's not the case! TabSwitch will preserve the project scope of the tabs. So if you reach the last tab of one project it will switch to the first tab of the next project and vice versa. Also TabSwitch will stay within the project tabs if you switch the tabs in a project tab group. Here's a picture which illustrates the behavior of TabSwitch:<br />
</p><div class="separator" style="clear: both; text-align: center;"><a href="https://raw.github.com/Yserz/TabSwitch/master/doc/TabSwitchTabs.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://raw.github.com/Yserz/TabSwitch/master/doc/TabSwitchTabs.gif" alt="Behavior of TabSwitch"/></a></div><p>You can download TabSwitch <br />
from NetBeans Plugin Center <a href="http://plugins.netbeans.org/plugin/54634/?show=true">here</a> or <br />
from the repository <a href="https://bitbucket.org/api/1.0/repositories/Yserz/ownmavenrepo/raw/HEAD/de/yser/TabSwitch/0.1/TabSwitch-0.1.nbm" download>here</a>.<br />
</p><br />
I hope you like it and...<br />
<br />
<br />
Happy Coding!Anonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.com0tag:blogger.com,1999:blog-5160788627603620390.post-47937129249511031752014-04-24T04:53:00.001-07:002014-04-30T13:41:54.957-07:00Filter Properties and pass them into Java Apps with Maven<ul><li><a href="https://github.com/Yserz/BeanBelt/tree/master/BBPassingPropertiesWithMaven">Sourcecode on GitHub</a></li>
</ul><p>If you create an app that will connect to other secured services, the day will come you have to add some developer keys or passwords. Or you want to simply define different environments (production, development, test) for your app running into e.g. for testing purposes. Changing that properties every time or pushing that properties to the repository would be a flaw. <br />
</p><b>Seriously, you should never push passwords etc. to your repository, no matter if it's private or not!</b><br />
<p>So what to do to prevent this? Passing that properties with Maven is one solution. Let's start! <br />
Create a <b>Maven Web Application Project</b> if you haven't and open your <code>pom.xml</code> You will probably see a <code>properties</code> section, if not create one and add these properties:<br />
</p><b>./pom.xml</b><br />
<pre class="brush: xml"><properties>
...
<jsf.projectstage>Development</jsf.projectstage>
<property.2>default2</property.2>
</properties>
</pre><p>Now go to the <code>build</code> section and add<br />
</p><b>./pom.xml</b><br />
<pre class="brush: xml"><build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
</pre><p>also add the following plugin to the <code>build</code> -> <code>plugins</code> section<br />
</p><b>./pom.xml</b><br />
<pre class="brush: xml"><build>
...
<plugins>
...
<plugin>
<groupid>org.apache.maven.plugins</groupId>
<artifactid>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
...
<plugin>
<groupid>org.apache.maven.plugins</groupId>
<artifactid>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failonmissingwebxml>false</failOnMissingWebXml>
<webresources>
<webresource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<targetpath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
</configuration>
</plugin>
...
</plugins>
</build>
</pre><p>Now the whole maven-part is done but we need some files to test our configuration.<br />
Create a <code>web.xml</code> file under <code>src/main/webapp/WEB-INF/</code> and fill in the following:<br />
</p><b>src/main/webapp/WEB-INF/web.xml</b><br />
<pre class="brush: xml">...
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name><param-value>${jsf.projectstage}</param-value></context-param>
<!-- JSF -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
...
</pre><p>Also add a <code>index.xhtml</code> file within <code>src/main/webapp/</code> with the following code:<br />
</p><b>src/main/webapp/index.xhtml</b><br />
<pre class="brush: xhtml"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>#{facesContext.application.projectStage}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div>
Project Stage: #{facesContext.application.projectStage}
</div>
</body>
</html>
</pre><p>Now you can Clean & Build the application and deploy it on your Application Server. The starting page <code>index.xhtml</code> will show you your actual <b>project stage</b>. The app will get the property <code><jsf.projectstage>Development</jsf.projectstage></code> from Maven and pass it into the <code>web.xml</code>. The <code>web.xml</code> is a <b>Web Resource</b> so the <code>maven-war-plugin</code> is responsible for that. Thus you can quickly change the JSF-Environment of your JSF-Application in the <code>properties</code> section of the Maven <code>pom.xml</code>. In addition you can pass that property when you trigger the build. For this simply execute the following line on the Terminal in your project dir:<br />
</p><pre class="brush: bash">mvn clean install -Djsf.projectstage=Production
</pre><p>Now we know the <code>maven-war-plugin</code> filters the <code>web.xml</code> but whats with that other stuff (<code>maven-resources-plugin</code> etc.)? With that you can filter not just <b>Web Resource</b>, you can filter <b>usual resources</b>. So let's do it. Create a <code>filtered_resource.properties</code> file under <code>src/main/resources/</code><br />
</p><b>src/main/resources/filtered_resource.properties</b><br />
<pre class="brush: java">filtered_value = ${property.2}
</pre><p>Now we need something to read the property. For that we create a Stateless EJB:<br />
</p><b>src/main/java/de/beanbelt/bbpassingpropertieswithmaven/OurService.java</b><br />
<pre class="brush: java">@Stateless
public class OurService {
public void readProperties() throws IOException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("filtered_resource.properties");
Properties properties = new Properties();
properties.load(stream);
String value = properties.getProperty("filtered_value", "not_filtered");
System.out.println("Value: " + value);
}
}
</pre><p>When you call the <code>readProperties()</code> method it will print the following on the console if everything is okay:<br />
</p><pre class="brush: java">Reading properties...
Value: default2
</pre>...if not it will print:<br />
<pre class="brush: java">Reading properties...
Value: not_filtered
</pre><p>That's it! Now you can pass properties into your Java Apps with Maven.<br />
</p>Happy Coding!Anonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.com0tag:blogger.com,1999:blog-5160788627603620390.post-26269566167634414072014-04-22T07:10:00.002-07:002014-04-30T13:45:00.634-07:00Working with Java EE Interceptors and Repeatable Annotations<ul><li><a href="https://github.com/Yserz/BeanBelt/tree/master/BBInterceptorsAndReapeatableAnnotations">Sourcecode on GitHub</a></li>
</ul><p>Since Java EE 6 you can invoke Interceptors around method calls. With Interceptors you will add Aspect-oriented Programming (AoP) patterns to your Java EE project. Given you have a method bound to an Interceptor a method call will look like this: <br />
</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEvoj4Zw8k5IkXUoa44QVzTCkxf7HxNDVALCsLs6_DxYRiMgVz6h13_hP_Mf9h8vprtkNH9yNEOFF4r1NYCVJLx8JiTRXAs-6GFri04qkjtVafG1OvqPBTagp37jDDo3ZiMOfYhaCCu3s/s1600/Interceptors.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEvoj4Zw8k5IkXUoa44QVzTCkxf7HxNDVALCsLs6_DxYRiMgVz6h13_hP_Mf9h8vprtkNH9yNEOFF4r1NYCVJLx8JiTRXAs-6GFri04qkjtVafG1OvqPBTagp37jDDo3ZiMOfYhaCCu3s/s1600/Interceptors.png" alt="Overview and Behavior of Interceptors"/></a></div><p>To actually implement this behavior there are some simple steps to do. First implement an Interceptor: <br />
</p><pre class="brush: java">@javax.interceptor.Interceptor
public class OurInterceptor {
@javax.interceptor.AroundInvoke
public Object someMethodName(InvocationContext context) throws Exception {
// do something before method
System.out.println(">before");
//executing source method
Object result = context.proceed();
// do something after method
System.out.println("<after");
return result;
}
}
</pre><p>And register our Interceptor in the <code>beans.xml</code>: <br />
</p><pre class="brush: xml"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
<interceptors>
<class>de.beanbelt.bbinterceptorsandreapeatableannotations.interceptors.OurInterceptor</class>
</interceptors>
</beans>
</pre><p>Now you can simply invoke this Interceptor by annotating a class or method in e.g. an Stateless EJB. <br />
</p><pre class="brush: java">@Stateless
// interceptor will be invoked on every method in OurService
@javax.interceptor.Interceptors(OurInterceptor.class)
public class OurService {
// You can invoke an Interceptor on one method only
// Note: this will overwrite the class-level Interceptor
//@Interceptors(OurInterceptor.class)
public String getName(long id) {
System.out.println("method");
return database.get(id).getName();
}
}
</pre><p>If you execute the code the following will be printed to the console: <br />
</p><pre class="brush: java">>before
method
<after
</pre><p>Now we extends the example by our own annotations which will invoke the interceptor. Create an annotation that looks like the following: <br />
</p><pre class="brush: java">@Inherited
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OurAnnotation {
@Nonbinding
boolean someValue() default false;
}
</pre><p>And change the Interceptor like this: <br />
</p><pre class="brush: java">@OurAnnotation
@javax.interceptor.Interceptor
public class OurInterceptor{...}
</pre><p>Now we can use the interceptor on method-level in OurService like this: <br />
</p><pre class="brush: java">@Stateless
public class OurService {
@OurAnnotation(someValue=true)
public String getName(long id) {
System.out.println("method");
return database.get(id).getName();
}
}
</pre><p>As you can see, we can pass parameters to the Annotation which we can read in our interceptor like this: <br />
</p><pre class="brush: java">@OurAnnotation
@javax.interceptor.Interceptor
public class OurInterceptor{
@javax.interceptor.AroundInvoke
public Object someMethodName(InvocationContext context) throws Exception {
Method method = context.getMethod();
if (method.isAnnotationPresent(OurAnnotation.class)) {
boolean someValue = method.getAnnotation(OurAnnotation.class).someValue();
System.out.println("someValue is: "+someValue);
}
// do something before method
System.out.println(">before");
//executing source method
Object result = context.proceed();
// do something after method
System.out.println("<after");
return result;
}
</pre><p>Since we annotated our method with someValue=true the output on the console will look like this: <br />
</p><pre class="brush: java">someValue is: true
>before
method
<after
</pre><p>So far so good but what happens if we want to annotate our method multiple times like this? <br />
</p><pre class="brush: java">@OurAnnotation(someValue=false)
@OurAnnotation(someValue=true)
public String getName(long id) {...}
</pre><p>We have to create a grouping Annotation (Note the S in OurAnnotationS). <br />
</p><pre class="brush: java">@Inherited
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OurAnnotations {
@Nonbinding
OurAnnotation[] value();
}
</pre><p>Now we can annotate our method like this: <br />
</p><pre class="brush: java">@OurAnnotations({
@OurAnnotation(someValue=false),
@OurAnnotation(someValue=true)
}) public String getName(long id) {...}
</pre><p>In Java 8 we can use the Repeatable Annotations feature. To do so we just need to add an Annotation to @OurAnnotation <br />
</p><pre class="brush: java">@Inherited
@Repeatable(OurAnnotations.class)
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OurAnnotation {...}
</pre><p>Now we can annotate our method as we stated before: <br />
</p><pre class="brush: java">@OurAnnotation(someValue=false)
@OurAnnotation(someValue=true)
public String getName(long id) {...}
</pre><p>But what happened to the Interceptor, why is it not working anymore? Well, we have to implement a new interceptor for our grouping Annotation like this: <br />
</p><pre class="brush: java">@OurAnnotations
@javax.interceptor.Interceptor
public class OurGroupingInterceptor{
@javax.interceptor.AroundInvoke
public Object someMethodName(InvocationContext context) throws Exception {
Method method = context.getMethod();
if (method.isAnnotationPresent(OurAnnotations.class)) {
OurAnnotation[] ourAnnotations = method.getAnnotation(OurAnnotations.class).value();
for (OurAnnotation annotation : ourAnnotations) {
System.out.println("someValue is: "+ annotation.someValue());
}
}
// do something before method
System.out.println(">before");
//executing source method
Object result = context.proceed();
// do something after method
System.out.println("<after");
return result;
}
}
</pre><p>Now the output on the console will look like this: <br />
</p><pre class="brush: java">someValue is: false
someValue is: true
>before
method
<after
</pre><p>Now we successfully implemented an Interceptor with our own Annotation which we can repeat like we want. <br />
</p><br />
Happy Coding!Anonymoushttp://www.blogger.com/profile/16255744273968434298noreply@blogger.com1