2015-09-17

Create a custom Apache Karaf server

What you will do:
  • Create a custom Apache Karaf distribution using karaf-maven-plugin
  • Create a custom logo using maven-bundle-plugin
What you will learn:
  • Configure karaf-maven-plugin
  • Build a OSGi bundle using maven-bundle-plugin
  • Use maven-dependency-plugin for post-processing
This tutorial creates a custom Karaf 3 server called "Dekantar" using Maven. The procedure for Karaf 2 and 4 are similar.
Karaf 2: Coming soon
Karaf 3: https://github.com/juttayaya/karaf/tree/master/karaf3/custom-karaf
Karaf 4: Coming soon


Step 1: Import the Karaf 3 dependencies
Add the following entry in the dependencyManagement section of the pom.xml
<dependencyManagement>
<!-- Import Karaf POM to use the correct 
     version of the Karaf dependencies -->
<dependencies>
    <dependency>
        <groupId>org.apache.karaf</groupId>
        <artifactId>karaf</artifactId>
        <version>${karaf.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

Doing this will automatically configure the custom Karaf Maven build to use the correct version of dependencies libraries.

See https://github.com/juttayaya/karaf/blob/master/karaf3/custom-karaf/pom.xml for full example.

Step 2: Configure the karaf-maven-plugin
Add the following entry to the pom.xml to configure karaf-maven-plugin to build Karaf
<dependencies>
<dependency>
<!-- Scope is compile so all features (there is only one) are installed
     into startup.properties and
     the feature repo itself is not added in
     etc/org.apache.karaf.features.cfg file
-->
    <groupId>org.apache.karaf.features</groupId>
    <artifactId>framework</artifactId>
    <type>kar</type>
    <scope>compile</scope>
</dependency>
<dependency>
<!-- Scope is runtime so the feature repo is listed
     in etc/org.apache.karaf.features.cfg file,
     and features will installed into the system directory.
     This imports the standard Karaf features described at   https://repo1.maven.org/maven2/org/apache/karaf/features/standard/3.0.4/standard-3.0.4-features.xml
     These features are installed in the karaf-maven-plugin configuration
-->
    <groupId>org.apache.karaf.features</groupId>
    <artifactId>standard</artifactId>
    <classifier>features</classifier>
    <type>xml</type>
    <scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
    <groupId>org.apache.karaf.tooling</groupId>
    <artifactId>karaf-maven-plugin</artifactId>
    <configuration>
    <karafVersion>${karaf.version}</karafVersion>
    <!-- bootFeatures starts up these features when Karaf first boots up.
         The feature names are obtained from the features.xml in the
         dependencies section
    -->
    <bootFeatures>
        <feature>standard</feature>
        <feature>config</feature>
        <feature>ssh</feature>
    </bootFeatures>
    <!-- installedFeatures only installs the libraries but the user will
         have to start it up manually via Karaf command line feature:install
    -->
    <installedFeatures>
        <feature>scheduler</feature>
        <feature>war</feature>
    </installedFeatures>
    </configuration>
</plugin>
</plugins>
</build>


Step 3: Create customized logo
You can customize the logo displayed when users first log in via command line

A.) Create the file src/main/resources/org/apache/karaf/branding/branding.properties
This file configures the welcome logo and command prompt.

B.) Configure maven-bundle-plugin to create an OSGi bundle with the branding.properties file.  Add the following entry in the pom.xml
<build>
<plugins>
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <configuration>
    <instructions>
        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
        <Import-Package>*</Import-Package>
        <Private-Package>!*</Private-Package>
        <Export-Package>org.apache.karaf.branding</Export-Package>
        <Spring-Context>*;public-context:=false</Spring-Context>
    </instructions>
    </configuration>
</plugin>
</plugins>
</build>

The maven-bundle-plugin creates a JAR file with a META-INF/MANIFEST.MF and branding.properties .

See https://github.com/juttayaya/karaf/blob/master/karaf3/custom-karaf/custom-karaf-branding/pom.xml for full example.

C.) Configure maven-dependency-plugin to copy the branding JAR created in step B into the Karaf lib directory.  Add the following entry to the pom.xml
<build>
<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <inherited>false</inherited>
    <executions>
        <execution>
        <id>copy</id>
        <phase>generate-resources</phase>
        <goals>
        <goal>copy</goal>
        </goals>
        <configuration>
        <artifactItems>
        <artifactItem>
        <groupId>${project.groupId}</groupId>
        <artifactId>custom-karaf-branding</artifactId>
        <version>${project.version}</version>
        <!-- target/assembly is the directory karaf-maven-plugin
  assembles all the files of the Karaf server -->
        <outputDirectory>target/assembly/lib</outputDirectory>
        <destFileName>custom-karaf-branding.jar</destFileName>
        </artifactItem>
        </artifactItems>
        </configuration>
        </execution>
    </executions>
</plugin>
</plugins>
</build>

See https://github.com/juttayaya/karaf/blob/master/karaf3/custom-karaf/custom-karaf-server/pom.xml for full example.

Step 4: Build it
Run mvn clean install
The output file is target/custom-karaf-server.tar.gz and target/custom-karaf-server.zip

2014-01-14

Apache ServiceMix intro

Apache ServiceMix (http://servicemix.apache.org/) is a great integration container that makes processing and transforming data from different sources easy. For example, if you need to constantly poll a directory for a CSV file and then process it into the database, Apache ServiceMix makes that process easy to create.

List of supported ServiceMix endpoints.

This is a quick cheat sheet on how to set-up and use Apache ServiceMix. The sample project will show how to use Apache ServiceMix.

Installation
1.) Download the default or full assembly at the ServiceMix download page.
2.) Uncompress it into any directory. The rest of the document will refer to this directory as /SERVICEMIX_DIR

      gunzip apache-servicemix.tar.gz
   tar xvf apache-servicemix.tar

Starting
1.) From a command prompt, go to directory /SERVICEMIX_DIR/bin

         cd /SERVICEMIX_DIR/bin
2.) Run command

         ./start

Deploying App
To deploy app, just copy the physical application into the directory /SERVICEMIX_DIR/deploy

That will automatically deploy the app.

Command Line Console
To connect to a local ServiceMix server with the command line console, use the command

cd /SERVICEMIX_BIN/bin
./client -a 8101 -h localhost -u smx -p smx


(These are the default settings for ServiceMix)

You will see a screen like


Web Console
The ServiceMix web console is not activated by default. To activate it for the first time, log into the command line console. Then from the ServiceMix console prompt, type the command

features:install webconsole

Then from a web browser, go to the URL http://localhost:8181/system/console

Stopping
To log out of the command line console without stopping ServiceMix, type in the command

shell:logout

To shutdown ServiceMix from the command line console, type in the command

osgi:shutdown now

ServiceMix can also be shut down from the operating system shell with the command

cd /SERVICEMIX_BIN
./stop

2014-01-12

How to add a new user/schema in Oracle

Question:
How do add a new user and schema in Oracle? (In Oracle, when you create a new user, you automatically create a new schema that has the same name as the username)

Answer:
Log in as a SYSTEM Oracle user and run the commands:

CREATE USER username
    IDENTIFIED BY password
    DEFAULT TABLESPACE user_tablespace
    QUOTA UNLIMITED ON user_tablespace
    TEMPORARY TABLESPACE temporary_tablespace;
 
GRANT CREATE SESSION TO username;
GRANT CREATE TABLE TO username;
GRANT CREATE VIEW TO username;
GRANT CREATE TRIGGER TO username;
GRANT CREATE PROCEDURE TO username;
GRANT CREATE SEQUENCE TO username;
GRANT CREATE SYNONYM TO username;
GRANT CREATE TYPE TO username;
GRANT UNLIMITED TABLESPACE TO username;

Oracle Doc on CREATE USER

You can find the default user_tablespace and temporary_tablespace via the SQL

SELECT *
FROM sys.database_properties
WHERE property_name like '%TABLESPACE%';


Question:
How do I delete a user?

Answer:

DROP USER username CASCADE;

2014-01-10

JEE Webapp Troubleshooting Tool

To help troubleshoot a web application, it is useful to print out information about the JEE server and the HTTP request.

I created a JSP page called serverinfo.jsp to display information about the server, Java memory, classpath, HTTP Session, and cookie.

I have used this tool numerous times in production to troubleshoot issues with load balancing, dropped sessions, bad cookies, and OutOfMemoryExceptions.

The source code is on GitHub .

View output of JSP page at http://jj-serverinfo.elasticbeanstalk.com/serverinfo.jsp