Programming Notes

Installing Oracle Database in Vagrant VM Using Puppet

| Comments

Vagrant enbles creating virtual machines easily, Puppet helps to setup Linux machine. In this example we will install Oracle database on Oracle Enterprise Linux Vagrant box using Puppet. Before we continue, Vagrant must be setup on your machine. There are a lot of tutorials on how to install Vagrant, one of the best is Vagrant official site. Also you need VirtualBox installed. Once you can see your Vagrant version (using vagrant --version command) and VirtualBox installed you can continue with this tutorial. So, in order to create new VM with Oracle database follow these steps:

  1. Download and unzip file from here or clone it from my GitHub repo https://github.com/paykin/vagrant-oracle-11g to your hard disk.

  2. Download Oracle Database installation from Oracle downloads site. For this tutorial we need Oracle Database 11g Release 2 (11.2.0.1.0) for Linux 64bit (Linux x86-64). There are 2 files to download. You need to place them to vagrant-oracle-11g/modules/oracle/files directory. While it is downloading, proceed to next step.

  3. In Vagrant file you may set machine host name, amount of memory

  4. In manifests/base.pp you can set Oracle database parameters

  5. In command line, go to directory you extracted attached file and write vagrant up.

That’s it. Installation will take a while, on my machine it too about 10 minutes. When it finishes, you can connect to the machine via SSH, issuing vagrant ssh command. If you prefer using standard ssh, you can connect to it with ssh vagrant@localhost -p 2222 command. Vagrant default root password is vagrant. In order to validate that everything works, you can run sqlplus / as sysdba and execute some simple query, say SELECT SYSDATE FROM dual. If you see current date – everything works ok. Also you can connect using SqlDeveloper or any othe client.

Liferay Portlets Deployment in IntelliJ IDEA

| Comments

You can develop Liferay portlets with IntelliJ IDEA exactly as in Eclipse Liferay IDE. Follow simple steps:

  1. Create debug configuration in IntelliJ IDEA using Local Tomcat
  2. Configure deployment artifact: add your WAR as yourwar:war exploded and set Application context to /yourwar
  3. Create XML file with name “yourwar.xml” with the following content:
    yourwar.xml
    1
    2
    3
    4
    5
    
    <?xml version="1.0" encoding="UTF-8"?>
    <Context
      docBase="/absolute/path/to/project/yourwar"
      reloadable="false"
      path="/yourwar"/>
    
    Please note, docBase have to be the same as “Output directory” setting in ItelliJ IDEA of the artifact you deploy. Also, path have to be the same as “Application context” in Run/Debug configuration
  4. Create post deployment action that will copy just created XML to Liferay’s autodeploy folder.
  5. As Liferay official wiki suggests, add empty CATALINA_BASE environment variable.

Enjoy automatic deployment, hot-swap and debugging your LIferay application.

Please note, I tested it with IntelliJ IDEA 12.1 and Liferay 6.0.5 on Tomcat 6

Integrating HornetQ With Camel and Spring

| Comments

Since Apache Camel have only one built-in implementation for specific JMS provider – ActiveMQ, you have to use generic org.apache.camel.component.jms.JmsConfiguration. In order to work with HornetQ using Camel and Spring first you have to add HornetQ dependencies to Maven pom.xml:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<dependency>
  <groupId>org.hornetq</groupId>
  <artifactId>hornetq-jms</artifactId>
  <version>${hornetq.version}</version>
</dependency>
<dependency>
  <groupId>org.hornetq</groupId>
  <artifactId>hornetq-jms-client</artifactId>
  <version>${hornetq.version}</version>
</dependency>
<dependency>
  <groupId>org.hornetq</groupId>
  <artifactId>hornetq-core-client</artifactId>
  <version>${hornetq.version}</version>
</dependency>
<dependency>
  <groupId>org.jboss.netty</groupId>
  <artifactId>netty</artifactId>
  <version>3.2.7.Final</version>
</dependency>

After that modify your spring.xml. Here is an example:

Spell Checking With Suggestions With Hibernate Search 4 and Lucene 3.6

| Comments

In latest releases of Hibernate Search 4 and Lucene 3.6 there was some changes in SpellChecker API’s. Here is example of the new API that allows to use spell checking with suggested words:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public String[] getSuggestions(String txt){

  String[] suggestions = new String[]{};
  FullTextSession fullTextSession = Search.getFullTextSession(sf.getCurrentSession());
  SearchFactory searchFactory = fullTextSession.getSearchFactory();
  IndexReader reader = searchFactory.getIndexReaderAccessor().open(MyEntity.class);

  try {
      FSDirectory spellCheckerDir = FSDirectory.open(new File("D:\lucene\spellchecker\com.site.model.MyEntity"));
      SpellChecker spellChecker = new SpellChecker(spellCheckerDir);
      
      Dictionary dictionary = new LuceneDictionary(reader, "description");
      IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, searchFactory.getAnalyzer("myAnalyzer"));
      spellChecker.indexDictionary(dictionary, config, true);

      suggestions = spellChecker.suggestSimilar(txt, 10);
  } catch (Exception e) {
      e.printStackTrace();
  }
  finally{
      searchFactory.getIndexReaderAccessor().close(reader);
  }
  return suggestions;
}

sf.getCurrentSession() gets standard Hibernate session. If you use EntityManager, there exists a method to obtain FullTextSession. The example creates new index directory dedicated to spell checking. It adds terms from MyEntity’s description field to the index. spellChecker.indexDictionary adds the terms to the index, and optionally merges it with existing index.

Portlets With Wicket 1.5.x in Liferay Portal

| Comments

After Wicket dropped support for portlets and moving it to wicketstuff the settings in XMLs are changed:

portlet.xml should look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<portlet>
  <description>wickettest4</description>
  <portlet-name>wickettest4</portlet-name>
  <display-name>wickettest4</display-name>
  <portlet-class>org.apache.wicket.portlet.WicketPortlet</portlet-class>
  <init-param>
      <name>wicketFilterPath</name>
      <value>/bla</value>
  </init-param>
  <supports>
      <mime-type>text/html</mime-type>
      <portlet-mode>VIEW</portlet-mode>
  </supports>
  <portlet-info>
      <title>wickettest4</title>
      <keywords>wickettest4</keywords>
  </portlet-info>
</portlet>

Note new portlet-class. Also, mime-type have to be text/html.

Create Datasource Programmatically on JBoss 7

| Comments

In order to create new datasource programmatically on JBoss 7 without restart (on the fly) from Java you can use CLI Java API. You have to include jboss-as-controller-client to your project dependencies:

1
2
3
4
5
<dependency>
  <groupId>org.jboss.as</groupId>
  <artifactId>jboss-as-controller-client</artifactId>
  <version>7.0.2.Final</version>
</dependency>

The following example creates new datasource:

Cleaning Eclipse With -clean Argument

| Comments

For those of you who work a lot with Eclipse, you probably run into all sorts of strange errors in Eclipse that not really related to your code, and then you get the feeling that Eclipse has gone mad.

One way to solve these problems and clean the Eclipse is using -clean argument. all you need to do is to edit the eclipse.ini file located in your and add it as the first argument on the first line. After you upload the eclipse you can delete it until the next time you will need it.

Forcing JBoss 7 to Apply Changes to JSP’s Immediately

| Comments

JBoss AS 7 by default do not reflect any changes to JSP files in WAR deployment. So, during application development you have to redeploy WAR or restart JBoss. In order to change this behaviour it is necessary to change section of urn:jboss:domain:web:1.0 in standalone.xml so it looks like:

standalone.xml
1
2
3
<configuration>
    <jsp-configuration development="true"/>
</configuration>

And now JBoss behaves as you expect it in development mode: all changes to JSP files are loaded automatically as soon as you change the JSP file.

Android Browser Recognizes CSS Media Type After Page Reloading

| Comments

While trying to optimize site for mobile devices you have to add viewport meta tag:

1
2
<meta name="viewport"
  content="target-densitydpi=device-dpi, width=device-width, height=device-height, initial-scale=1.0" />

You can add additional CSS file that will be used on mobile device only. To achieve this just add CSS media type. In Android developer’s guide at http://developer.android.com/guide/webapps/targeting.html it suggests to target device DPI value like:

Enums in JSF

| Comments

Finally I find the way to use enum with JSF: Suppose you have enum:

1
2
3
4
5
public enum ActionTypeEnum {
  UPDATE,
  CREATE,
  COPY
}

In JSF you can write EL expressions in form:

1
2
<h:outputText value="someText"
  rendered="#{listAction.actionType == 'COPY'}"/>

The property actionType in managed bean listAction have to be of type ActionTypeEnum. It works because each enum in have method valueOf(String stringValue) and JSF attempts to convert String value to enum.