Thursday, November 1, 2012

IF ELSE statement in oracle SELECT QUERY

Every time I use and forget the semantics of following two sql queries. And I think the queries are self explanatory.
SELECT emp_name, 
    CASE WHEN emp_type = 0 THEN 'Temporary'
         WHEN emp_type > 0 AND emp_type < 5 THEN 'Permanent'
         ELSE 'UNKNOWN'
    END AS emp_type
FROM employee;
SELECT emp_name, 
    DECODE(emp_type, 0, 'Temporary',
                     1, 'Permanent',
                     2, 'Permanent',
                     3, 'Permanent',
                     4, 'Permanent',
           'UNKNOWN')AS emp_type
FROM employee;

Thursday, May 31, 2012

WebLogic deployer: EAR deploy script for weblogic server.

#!/bin/sh

git pull

ant clean ear

WL_PATH=<wl-server>/lib/weblogic.jar
WL_URL=http://localhost:7001/

java -cp $WL_PATH:$CLASSPATH weblogic.Deployer 
     -adminurl $WL_EVR 
     -username weblogic 
     -password weblogic 
     -name my_project 
     -deploy dist/my_project.ear
Before Running this script check if tunneling is no.

Environment->servers->[server-name]->Protocols
'Enable Tunneling' flag should be ON.

WebLogic server: redirect console output to file.

Set following variable with the file path into startWebLogic.sh.
# Call setDomainEnv here.

WLS_REDIRECT_LOG=/tmp/myproject-console.log

Monday, May 28, 2012

WebLogic server Error: Could not obtain an exclusive lock for directory.

Steps to get out of this situation:

1. Shutdown the server.
2. Delete the lok file from <domain-home>/servers/<server-name>/tmp/.
3. Try to start the server.
4. If the server still fails to start then find the process which is still running on this server using following commands.
ps -ef | grep 'weblogic'
OR ps -ef | grep 'oracle'
kill -9 PID
4. Now start the server.

Wednesday, April 4, 2012

WenLogic: EJB service ERROR: Two classes have the same XML type name

<Apr 4, 2012 3:39:04 PM BDST> <Error> <HTTP> <BEA-101216> <Servlet: "WSEE_SERVLET" 
failed to preload on startup in Web application: "/HelloEjbServiceBean".
javax.xml.ws.WebServiceException: Unable to create JAXBContext
at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:164)
at com.sun.xml.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:94)
at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:265)
at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:363)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:202)
Truncated. see log file for complete stacktrace

Caused By: java.security.PrivilegedActionException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:
1 counts of IllegalAnnotationExceptions
Two classes have the same XML type name ....

This is a common EJB error for the beginners like me..;)...Very often I forget the fact that EJB creates an XML type with the name [METHOD+"Response"] so there should not be any bean for service request/response with  this name.

Monday, February 20, 2012

WebLogic ERROR: No credential mapper entry found for password indirection

To solve this error follow the steps bellow..

1. Disable following flag form JDev -> application property -> deployment
Uncheck "Auto Generate and Synchronize weblogic-jdbc.xml..."

2. Carefully put the jta-data-source name in your persistence.xml.
<jta-data-source>jdbc/hello_ds</jta-data-source>
<properties>
      <property name="eclipselink.target-server" value="WebLogic_10"/>
      <property name="javax.persistence.jtaDataSource"
                value="jdbc/hello_ds"/>
</properties>
Best way to ensure this is by modifying the EAR archive.

3. Create a data source into your server and put the exact JNDI name as your jta-data-source name.

Wednesday, February 15, 2012

Spring 3 : Servlet Context and other Context beans.

Here I will try to explain following question about ServletContext and Other Context beans in a spring application.

1. Why do we need a ServletContext?
2. Why do we need an ApplicationContext?
3. Do we need them both in a spring 3 application?
4. Most Common use of a ServletContext.
5. Most Common use of an ApplicationContext.

ServletContext:
For any spring application a servlet context is must which we define in spring-servlet.xml or myapp-servlet.xml file. When a servlet starts it looks for its context definition and load the context. Most of the cases we define following components in a servlet context.

<!-- Notify the servlet to load the components using annotation, 
for example '@controller' -->
<mvc:annotation-driven/>

<!-- Load the beans/controllers into context from base-package and 
Map requests from '@RequestMapping' annotation. -->
<context:component-scan base-package="com.mycompany.helloSpring3.web" />

<!-- This defination is used to create ModelAndView object by servlet. -->
<bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
</bean>
ApplicationContext:
This is an optional Context. In fact there may multiple context files in an application as per need. Usually we put the dataSource related definitions in this file.

Note: If we need to use additional Context files along with the ServletContext, In that case we need to tell our servlet-descriptor(web.xml) to load them with a ContextLoaderListener.
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/*Context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Sunday, January 29, 2012

WebLogic Error: java.lang.ClassNotFoundException: oracle.security.jps.wls.listeners.JpsApplicationLifecycleListener

If I try to deploy an EJB service application on WebLogic 10.3, I get following error message.
java.lang.ClassNotFoundException: oracle.security.jps.wls.listeners.JpsApplicationLifecycleListener
Reason: JDeveloper deployment wizard assumes any EJB application will have jsp pages so it adds JSP listeners declaration. But if we do not use jsp then we don't need the ADF faces libraries integrated.
Here is the solution of this problem.
Modify following xml file inside the EAR archive using archive manager.
/META-INF/weblogic-application.xml
Remove following declarations from the xml.
<listener>
    <listener-class>oracle.security.jps.wls.listeners.JpsApplicationLifecycleListener</listener-class>
</listener>
<listener>
    <listener-class>oracle.security.jps.wls.listeners.JpsAppVersionLifecycleListener</listener-class>
</listener>

Wednesday, January 25, 2012

Android SQLite database performance improvement for inserting rows.

I was trying to insert 2000 rows into a SQLite table and following code took almost 30s in emulator and few minuets on device which of course varies.
/* db helper class */
public void insert(ContentValues cv) throws RuntimeException{
    db.insert("my_table", null, cv);
}


/* activity class*/
while(for each rows)
  insert(data);
After handling the transaction manually for all the rows at a time the performance improved drastically and here is the code.
public void insert(List dataArr) throws RuntimeException{
    db.beginTransaction();
    for(int i=0, j=dataArr.size(); i<j; i++) {
        ContentValues cv = (ContentValues)dataArr.get(i);
        db.insert("my_table", null, cv);
    }
    db.setTransactionSuccessful();
    db.endTransaction();
}

Getting data from URL in java

public String getDataFromURL(String urlStr) throws Exception {
   URL url = new URL(urlStr);
   URLConnection urlCon = url.openConnection();
   BufferedReader in = new BufferedReader(
                           new InputStreamReader(
                           urlCon.getInputStream()));
   String data = "";
   String line = "";
        
   while ((line = in.readLine()) != null)
          data += line;

   in.close();
   return data;
}

Parsing JSON data in java

public void parseJSONObject(String jsonData) throws Exception {
    JSONTokener jsonTokener = new JSONTokener(jsonData);
    JSONObject object = (JSONObject) jsonTokener.nextValue();
    object.getString("id");
}

public void parseJSONArray(String jsonData) throws Exception {
    JSONTokener jsonTokener = new JSONTokener(jsonData);
    JSONArray arr = (JSONArray) jsonTokener.nextValue();
    for(int i=0, j=arr.length(); i < j; i++) { 
        JSONObject object = arr.getJSONObject(i);
        ...
    }
}

Monday, January 9, 2012

WebLogic server domain creation

Goto WebLogic server home directory
in my case
/home/shamim/Oracle/Middleware/wlserver_10.3
Run
./common/bin/config.sh
Type [1] to choose Create a new WebLogic domain
Type [1] to Choose Weblogic Platform components. You can also choose [2] the custom template option.
Application Template Selection:
In my case I want deploy EJB service beans on webLogic server so I choose Basic WebLogic Server Domain and Type [Next]
Edit Domain Information:
the default domain name is 'base_domain' you can change the name or type [Next]
Select the target domain directory for this domain:
you can change the location or type [Next] to use default one.
Configure Administrator User Name and Password:
The default user name for Administrator user is 'weblogic'. Type [1] to change this or not.
Type [2] to set "User password". Minimum length of the password should be 8. This password will be used to login into console.
Type [3] to confirm password.
Type [Next]
Domain Mode Configuration:
Type [1] for Development Mode which dose not include some security features.
Java SDK Selection:
You should see a list of available JDK and choose one from them.
Select Optional Configuration:
Type [1] because if you have only one server it should be the Administration Server.
Configure the Administration Server:
...
Remember the port number and type [Next] if you don't like to change it.

In few seconds domain creation should be completed. And you will find the server instance ready for use into server domain. in my case [/home/shamim/Oracle/Middleware/user_projects/domains/ejb_services] this is the admin server home.
Following command will start the server.
./bin/startWebLogic.sh
Console url
http://localhost:7001/console

WebLogic server installation guide for Debian 6 (64-bit)

Download Web Logic Server from:

1. Weblogic server Installers from here
OR
2. JDevStudio which includes JDeveloper IDE and WebLogic Server, download.

Run The installer:
    In my case [./jdevstudio11113install.bin]
    You should see this message
    [Unable to instantiate GUI, defaulting to console mode.]
    Type [Next]

Choose Oracle Middleware Home Directory:
    You can change the default home dir or not.

Choose Install Type:
    For Complete installation type [1]
    For custom installation type [2] in this case you add/remove components from installation

Confirm Product Installation Directories:
    Remember the server home directory and type [Next].

The following Products will be installed:
    ...
    Type [Next]

Installation should start and complete in few moments and we not done yet. To start the WebLogic server we need to create an instance first. It is called domain which creates an instance of the server for use. You can see more information about domain from here. Please see my next post for creating an WebLogic domain.

Sunday, January 8, 2012

iPhone like segment-control button for Android


This implementation of segment buttons for android is based on custom styled button. Here four button styles are used for button's state and position changes. And With a few tweaks you can add more buttons to the group.
Activity Code:
offlineBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onlineFlag = 0;
offlineBtn.setBackgroundResource(R.drawable.segment_button_left_on);
onlineBtn.setBackgroundResource(R.drawable.segment_button_right);
}
});

onlineBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onlineFlag = 1;
offlineBtn.setBackgroundResource(R.drawable.segment_button_left);
onlineBtn.setBackgroundResource(R.drawable.segment_button_right_on);
}
});

Layout(View) File:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"    
    android:layout_marginTop="0dip"  android:layout_marginBottom="0dip" 
    android:orientation="horizontal" android:baselineAligned="false">
        <Button android:id="@+id/offline_button" 
        android:layout_height="wrap_content" 
        android:text="Offline" android:textColor="#ffffff"    
                android:textSize="13dip" android:singleLine="true"
        android:background="@drawable/segment_button_left_on"
        android:layout_marginTop="5dp" android:layout_width="wrap_content">
        </Button>
        <Button android:id="@+id/online_button" 
        android:layout_height="wrap_content" 
        android:text="Online" android:textColor="#ffffff"    
                android:textSize="13dip" android:singleLine="true"
        android:background="@drawable/segment_button_right"
        android:layout_marginTop="5dp" android:layout_width="wrap_content">
        </Button>
</LinearLayout>

res/drawable-hdpi/segment_button_left_on.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#7E3117" android:startColor="#7E3817"
android:angle="270" />
<stroke android:width="1dp" android:color="#646060" />
<corners android:topLeftRadius="6dp" android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="6dp" android:topRightRadius="0.1dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>

<item>
<shape>
<gradient
android:startColor="#99009900" android:endColor="#FF009900"
android:angle="270" />
<stroke android:width="0.5dp" android:color="#646060" />
<corners android:topLeftRadius="6dp" android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="6dp" android:topRightRadius="0.1dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>
</selector>

res/drawable-hdpi/segment_button_left.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#7E3117" android:startColor="#7E3817"
android:angle="270" />
<stroke android:width="1dp" android:color="#646060" />
<corners android:topLeftRadius="6dp" android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="6dp" android:topRightRadius="0.1dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>

<item>
<shape>
<gradient
android:startColor="#991A1111" android:endColor="#991A1111"
android:angle="270" />
<stroke android:width="0.5dp" android:color="#646060" />
<corners android:topLeftRadius="6dp" android:bottomLeftRadius="0.1dp"
android:bottomRightRadius="6dp" android:topRightRadius="0.1dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>
</selector>

res/drawable-hdpi/segment_button_right_on.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#7E3117" android:startColor="#7E3817"
android:angle="270" />
<stroke android:width="1dp" android:color="#646060" />
<corners android:topLeftRadius="0.1dp" android:bottomLeftRadius="6dp"
android:bottomRightRadius="0.1dp" android:topRightRadius="6dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>

<item>
<shape>
<gradient
android:startColor="#99009900" android:endColor="#FF009900"
android:angle="270" />
<stroke android:width="0.5dp" android:color="#646060" />
<corners android:topLeftRadius="0.1dp" android:bottomLeftRadius="6dp"
android:bottomRightRadius="0.1dp" android:topRightRadius="6dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>
</selector>

res/drawable-hdpi/segment_button_right.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#7E3117" android:startColor="#7E3817"
android:angle="270" />
<stroke android:width="1dp" android:color="#646060" />
<corners android:topLeftRadius="0.1dp" android:bottomLeftRadius="6dp"
android:bottomRightRadius="0.1dp" android:topRightRadius="6dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>

<item>
<shape>
<gradient
android:startColor="#991A1111" android:endColor="#991A1111"
android:angle="270" />
<stroke android:width="0.5dp" android:color="#646060" />
<corners android:topLeftRadius="0.1dp" android:bottomLeftRadius="6dp"
android:bottomRightRadius="0.1dp" android:topRightRadius="6dp"/>
<padding
android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
</shape>
</item>
</selector>