Sunday, August 3, 2014

GWT app engine Missing required argument 'module[s]'


If you get this exception while deploying your project into google app engine.
Then:
http://stackoverflow.com/questions/8628429/google-app-engine-missing-required-argument-modules


When creating a new Google Web Application project, there are 2 options, Google Web Toolkit and Google App Engine which can be selected. I was selecting both so the project which was being created was a GWT project. So while running it was expecting a gwt module and related configuration file.
The solution was to not select "Google Web Toolkit" when creating a new Google Web Application Project.

To disable Google Web Toolkit for your created project, just do like this: 1. select project, right click, and select "Property" 2. Toggle down "Google", select "Web Toolkit" tab, and uncheck "Use Google Web Toolkit". 3. Delete your project in Run configuration. And run again. You will find it never asks for "modules" anymore.

Thursday, December 5, 2013

Liferay admin detection, community admin, Organization admin



If you see any portlet, it used to have four button 1. Preference 2. Maximise 3. Minimise 4. Close.
Only those user who has admin privilege, can see those button or will have able to change preference.

If user has community admin/owner, organization admin/owner, they can also see those button on their respective page. So, there are many combination like
user will not have admin privilege but have community admin then which kind of button they can see.

Now, if you have to developer one button and condition is that only admin(super admin, comminity admin, organization admin) can see that button, then before showing
that button, you have to whether logged in user has admin facility or not.

Liferay has given one API by which you can easily find out, use below code:

public boolean isUserLiferayAdmin(){
      PortletContextImpl portletContext = (PortletContextImpl)context;
      ThemeDisplay themeDisplay= (ThemeDisplay)portletContext.getRequest().getAttribute(WebKeys.THEME_DISPLAY);
      PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();
      Long groupId = themeDisplay.getLayout().getGroupId();
      boolean isAnyLiferayAdmin = false;
       try {
   isAnyLiferayAdmin = GroupPermissionUtil.contains(permissionChecker,groupId, ActionKeys.MANAGE_LAYOUTS);
    }catch (Exception e1) {
     isAnyLiferayAdmin = false;
    }
      return isAnyLiferayAdmin;
   }


you can write this method in BaseComponent.java, so all the user defined portlet can user this method.

Other way is also there to determine admin previlages:

try{
    List roles = user.getRoles();
    String isAdmin  = "false";
    for(Role role:roles){
    if(role.getName().equals("Administrator")) {isAdmin = "true";break;}
    }
   session.setAttribute("USER_GROUP",isAdmin);
  }catch(Exception e){
   log.error("Not able to fetch Liferay user group info");
  }




Tuesday, June 25, 2013

java.lang.NullPointerException at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)


Very small post, actually I got this error (java.lang.NullPointerException at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)) while hitting REST web service. I searched a lot and spent all most one day but could not get reason behind it.

Finally one got solution, so thought to share with you, it may be helpful some time.

Jersey uses the thread context class loader to load such classes. Perhaps you can do the following:

Thread .currentThread ().setContextClassLoader(this.getClass().getClassLoader()); 

Put the above line code, just before you hit the REST.

For Example:
Thread .currentThread ().setContextClassLoader(this.getClass().getClassLoader());
webResource = client.resource("your REST server URL");
client.addFilter(new HTTPBasicAuthFilter("serverUserName", "serverPassword"));

if (response.getStatus() != 200) {
   throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

String output = response.getEntity(String.class);

If you some other solution, then please share.

Thanks.

Binod Suman

Tuesday, May 14, 2013

Google Apps Engine com.google.appengine.tools.admin.JspCompilationException: Failed to compile jsp files.


My Google Apps Engine was working fine till I installed Windows 7 (62 bit). After shifting to Windows 7, when I tried to deploy my GAE code to google, I got below error:


------------ Deploying frontend ------------

Preparing to deploy:
Created staging directory at: 'C:\Users\sbinod\AppData\Local\Temp\appcfg5079868666950863187.tmp'
Scanning for jsp files.
Compiling jsp files.
com.google.appengine.tools.admin.JspCompilationException: Failed to compile jsp files.
May 15, 2013 8:04:53 AM org.apache.jasper.JspC initClassLoader
WARNING: TLD files should not be placed in /WEB-INF/lib
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.jasper.JspCompilationContext.createCompiler(JspCompilationContext.java:238)
at org.apache.jasper.JspCompilationContext.createCompiler(JspCompilationContext.java:214)
at org.apache.jasper.JspC.processFile(JspC.java:1181)
at org.apache.jasper.JspC.execute(JspC.java:1341)
at com.google.appengine.tools.development.LocalJspC.main(LocalJspC.java:40)
Caused by: java.lang.NullPointerException
at com.google.appengine.tools.development.LocalJspC$LocalCompiler.(LocalJspC.java:53)
... 7 more

I tired many solution like to upgrade GAE SDK to 1.8 and re-installed Java 6 in different folder and change some script into appcfg.cmd, BUT did not worked.

Then I changed my laptop path variable and kept my jdk path before C:\Windows\system32.
So, simply I went to start -> Computer -> Right Click -> Properties - > Advanced System Settings ->
Environment Variable  -> Inside System Variable -> click on Path variable -> Click on Edit button -> Add these path (as per you system) C:\Java\jdk1.6.0_39\bin; at first. -> ok.
Restart Eclipse then try to deploy your code to Google.

Second thing, copy tools.jar from your java JDK (in my caseC:\Java\jdk1.6.0_39\lib\tools.jar) to your Apps Engine SDK (E:\Software\Google_Apps_Engine\appengine-java-sdk-1.8.0\appengine-java-sdk-1.8.0\lib\shared)

Other possible Solution:
There could be any real compilation issue in your JSP file, but while trying to build your project into google apps engine, problem would be show there, then follow these steps to know what exactly issue there:

run \bin\appcfg.cmd update  in a command window, it will provide you with a more detailed output then the plugin does and will most likely enable you to resolve the problem in a matter of minutes.

For Example:
E:\Software\NOC\Eclipse-Juno\eclipse\plugins\com.google.appengine.eclipse.sdkbundle_1.7.7.1\appengine-java-sdk-1.7.7.1\bin>appcfg.cmd update "E:\Binod\Demo\demogaeprojectWorkspace\LIC\war"

Dear Friend,
You can add your feedback if your problem solved by any other way.


Thursday, April 18, 2013

Ext JS getting start Ext JS first tutorial First Program for EXT JS




Please visit Ext JS site for more information and licence policy on http://www.sencha.com/

Ext js tutorial:

1. Download Ext js: http://www.sencha.com/products/extjs3/download/ and download ext-3.4.1.1-gpl.zip
2. Unzip it some where
3. Create one dynamic J2ee Project in Exlipse say demoExt and add one server
4. Now inside demoExt Project, create one folder inside the WebContent (demoExt\WebContent\extjs)
5. From unzip of ext-3.4.1.1-gpl.zip copy adapter, resources folder and ext-all.js and paste inside demoExt\WebContent\extjs
6. Create one jsp (index.jsp) inside the WebContent

contect of index.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Binod Here</title>



<meta charset=utf-8 />
 
  <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="extjs/ext-all.js"></script>

<script type="text/javascript" >
    Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';
</script>


<script type="text/javascript">
Ext.onReady(function(){

Ext.Msg.alert("Binod","This is Ext alert message");

});
</script>


</head>
<body>
<h4> Hi Binod from EXT JS</h4>
<%= new java.util.Date() %>
</body>
</html>

Now deploy this demoExt project in any webserver (inside Eclipse better) and hit the browser with below URL

http://localhost:8080/demoExt/

Now you will see one Ext JS alert box with message "This is Ext alert message".

Happy Ext JS coding ................ :)

That it ............




Monday, October 8, 2012

Simple RMI and RMI with Spring


RMI (=Remote Method Invocation)

The basic structure of an RMI-based method call involves a client, a server and a registry. To make a call to a remote object, the client first looks up the object it wishes to invoke a method on in the registry. The registry returns a reference to the object (assuming it exists) on the server, which the client can use to invoke any methods that the remote object implements.

Step1 : HelloInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloInterface extends Remote {
  public String say() throws RemoteException;
}


Step 2: Hello.java

import java.rmi.*;
import java.rmi.server.*;

public class Hello extends UnicastRemoteObject
 implements HelloInterface {
  private String message;
  public Hello (String msg) throws RemoteException {
  message = msg;
  }
  public String say() throws RemoteException {
  return message;
  }
}


Step3.Compile the above two Source file named HelloInterface.java and Hello.java.

Step4.After compiling the above two classes type the following command i.e-  "rmic Hello" in console
> rmic Hello

By running the "rmic Hello" command a new class will be created i.e "Hello_Stub.class" in the directory.

Step5.Create Server application named HelloServer.java

HelloServer.java

import java.rmi.Naming;

public class HelloServer
{
  public static void main (String[] argv)
  {
  try {
  Naming.rebind("Hello", new Hello ("Hello,From Roseindia.net pvt ltd!"));
  System.out.println ("Server is connected and ready for operation.");
  }
  catch (Exception e) {
  System.out.println ("Server not connected: " + e);
  }
  }
}


Step6.Create Client application named HelloClient.java

HelloClient.java

import java.rmi.Naming;

public class HelloClient
{
  public static void main (String[] argv) {
  try {
HelloInterface hello =(HelloInterface)
 Naming.lookup ("//192.168.10.201/Hello");
  System.out.println (hello.say());
  }
  catch (Exception e){
  System.out.println ("HelloClient exception: " + e);}
  }
}

Step6.Compile both of the files.

Step7.Type "rmicregistry" on commandprompt and press ENTER.



Step8.Type java HelloServer in commandprompt and press ENTER.The following message will be displayed on console.

Console output will come
Server is connected and ready for operation.

Step9.Now,open another separate command terminal,and run the client application like shown in the figure given below:-

javac HelloClient.java
java  HelloClient
Hello, From RoseIndia.net pvt ltd.

Step10. If the message similar to the above appears in figure comes means that you have implemented your RMI application.

In short:
Write the service implementation class with methods that throw java.rmi
.RemoteException.
2 Create the service interface to extend java.rmi.Remote.
3 Run the RMI compiler (rmic) to produce client stub and server skeleton classes.
4 Start an RMI registry to host the services.
5 Register the service in the RMI registry.


RMI using SPRING:

Fortunately, Spring provides an easier way to publish RMI services. Instead of writing RMI-specific classes with methods that throw RemoteException, you simply write a POJO that performs the functionality of your service. Spring handles the rest.

For a typical Spring Application we need the following files:

1. An interface that defines the functions.
2. An Implementation that contains properties, its setter and getter methods, functions etc.
3. A XML file called Spring configuration file.
4. Client program that uses the function

Instead of generating a server skeleton and client stub using rmic and manually adding it to the RMI registry (as you would in conventional RMI), we’ll use Spring’s RmiServiceExporter.

RmiServiceExporter exports any Spring-managed bean as an RMI service. RmiServiceExporter works by wrapping the bean in an adapter class. The adapter class is then bound to the RMI registry and proxies requests to the service class.

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="serviceName" value="employee-service"/>
        <property name="service" ref="employeeService"/>
        <property name="serviceInterface" value="rmi.common.EmployeeI"/>
        <property name="registryPort" value="1234"/>
</bean>

<bean id="employeeService" class="rmi.server.EmployeeImpl">
</bean>

public class Employee implements Serializable {
 private String name;
 private String address;

 public Employee(String name,String address){
  this.name = name;
  this.address = address;
 }

 // getters and setters
}


public interface EmployeeI {

 public void addEmployee(Employee employee);
 public void removeEmployee(Employee employee);
 public List<Employee> getEmployees();
   
}

public class EmployeeImpl implements EmployeeI{

    private List<Employee> employees = new ArrayList<Employee>();

    public void addEmployee(Employee employee) {
     employees.add(employee);
    }
   
    public void removeEmployee(Employee employee){
     employees.remove(employee);
    }

    public List<Employee> getEmployees() {
        return employees;
    }
}


Now to run the server side service you need Spring context initialization.


public class EmpServerDemo {
 public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi/server/rmi-server-context.xml");
 }
}


CLIENT SIDE

Now let us have a look at client side.

To link in the service on the client, we'll create a separate Spring container, containing the simple object and the service
linking configuration bits:


<beans>
    <bean id="employeeService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://localhost:1234/employee-service"/>
        <property name="serviceInterface" value="rmi.common.EmployeeI"/>
    </bean>
</beans>


You can make client calls through the below code...

public class EmpClientDemo {
 public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi/client/rmi-client-context.xml");
  EmployeeI employee = (EmployeeI) ctx.getBean("employeeService");
  employee.addEmployee(new Employee("Prashant", "address1"));
  employee.addEmployee(new Employee("Sneha", "address2"));
  List<Employee> employees = employee.getEmployees();
  System.out.println("Total number of employees: " + employees.size());
  Iterator<Employee> it = employees.iterator();
  while (it.hasNext()) {
   Employee emp = (Employee) it.next();
   System.out.println(" " + emp);
  }
 }
}









 UnicastRemoteObject : This is the simplest way to ensure that objects of a class can be used as remote objects.


Saturday, October 6, 2012

Command Design Pattern using simple example


Command Design Pattern:

Detail : http://en.wikipedia.org/wiki/Command_pattern


Use strategy when you need to define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients that use it.


Strategy pattern are algorithms inside a class which can be interchanged depending on the class used.
This pattern is useful when you want to decide on run time which algorithm to be used.

Calculation.java

public interface Calculation {
int execute(int a, int b);
}

AddCalc.java
public class AddCalc implements Calculation{
@Override
public int execute(int a, int b) {
return a+b;
}
}

SubCalc.java
public class SubCalc implements Calculation{
@Override
public int execute(int a, int b) {
return a-b;
}
}


MultiCalc.java
public class MultiCalc implements Calculation{
@Override
public int execute(int a, int b) {
return a*b;
}
}

DivideCalc.java
public class DivideCalc implements Calculation{
@Override
public int execute(int a, int b) {
if(b==0) {return 0;}
return a/b;
}
}



Test.java
Map commands = new HashMap();
public Test(){
        commands.put("add", new AddCalc());
        commands.put("sub", new SubCalc());
        commands.put("multi", new MultiCalc());
        commands.put("div", new DivideCalc());
}

public int calc(int first,int second,String operation){
  Calcuation cal = commands.get(operation);
 return cal.execute(first,second);
}