We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Serial Port Communication using EJB

VRGultom
VRGultom asked
on
Medium Priority
1,702 Views
Last Modified: 2010-08-05
Hi

I want to use EJB to open serial port and send/receive data through the port.

I have tried to make the code in java class. It is run properly. But when I convert it to EJB code. It did not work. The EJB is called but the code for open the port not execute.
Can anyone tell me how to make it work properly?

Here is my EJB Code:

***********************************
package SerialDemo.openPort;

import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import java.io.*;
import java.util.*;
import javax.comm.*;
import java.lang.Object;
import java.lang.String.*;
import java.security.*;
import java.math.*;

import java.rmi.*;
import javax.naming.*;

public class openportBean implements SessionBean
{         
    private SessionContext context;
   
      static CommPortIdentifier portId;
      static SerialPort serialPort;
      static OutputStream outputStream;
    static InputStream inputStream;             
    public Enumeration portList;
   
         
      public void checkport() throws RemoteException            
      {                  
      
            portList = BukaPort();
            
             while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
           
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {                
                    try {
                        serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);                                                
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                   
                    setting_port(serialPort);
                                     
                    send_data();                                      
                          
                                       
                    try
                    {
                              inputStream = serialPort.getInputStream();
                          }
                          catch (IOException e) {}
                   
                                       
            }
        }      
          
         } //public             
         
 
       static void setting_port(SerialPort serialPort)
    {
    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
      }         
       
      Enumeration BukaPort() throws RemoteException
      {
            portList = CommPortIdentifier.getPortIdentifiers();
            return portList;
      }
                  
      public void ejbCreate()
      {}
            
      public void ejbPassivate()
      {}
            
      public void ejbActivate()
      {
            //portList = CommPortIdentifier.getPortIdentifiers();
      }
      
      public void ejbRemove()
      {}
      
      //public void setSessionContext(SessionContext context)
      public void setSessionContext(SessionContext context)
      {}      
}

**********************
Thanks in advance

Veronika      
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi John,

You are right.

I have added the permission to my java.policy. But it is still error
java.io.FilePermission C:\JDK1.3.1_02\jre\lib\javax.comm.properties delete)

Could you tell me how to fix the problems?

Thanks
Veronika
What J2EE server are you using (WebLogic, WebSphere, JBoss)?

I think the .policy file you may need to fix will be in that tree (the server's tree), rather than for your JRE, but it depends on how things are set up.

Once you find the right file, you can add:

grant {
    permission java.io.FilePermission ""<<ALL FILES>>", "read,write,delete";
};

This can sometimes be a bit tricky -- on WebSphere, for instance, you may have to specify:

grant "file:yourapp.war" {
    permission java.io.FilePermission "/-", "read,write,delete";
};

NOTE:

This is saying that all apps running in your server have this permission, which is a security hole. If you want to restrict the permission only to a certain application, you may have to do a lot of extra work to figure out where the code is in the server's "repository."
Oops, typo. That should be only one " before <<ALL FILES>>:

grant {
    permission java.io.FilePermission "<<ALL FILES>>", "read,write,delete";
};

As I said, the syntax for this can be slightly different depending on the server, so once you find the right .policy file, you may need to read the doc for your particular server. Or tell us what server you're using.

Author

Commented:
I am using jdk1.3.1_02 and j2sdkee1.3.1 downloaded from java.sun.com
but I put the java.policy in <jdk>\jre\lib\security



OK.

For the j2sdkee1.3.1 (the J2EE "Reference Implementation") the file you want should be:

    j2sdkee1.3.1\lib\security\server.policy

Just add the following line at the end of server.policy, and restart your server.

    grant {
        permission java.io.FilePermission "<>", "read,write,delete";
    };

at the end of the file. It *may* be just

    grant {
        permission java.io.FilePermission "read,write,delete";
    };

Author

Commented:
Jhon,

I am not fix this probelm yet.
Now, the error message is:
java.lang.NullPointerException: name can't be null

Author

Commented:
Should I declare the portlist in static?

portList = CommPortIdentifier.getPortIdentifiers();

How about commportidentifier as static method?

what is the relationship?

Author

Commented:
Should I declare the portlist in static?

portList = CommPortIdentifier.getPortIdentifiers();

How about commportidentifier as static method?

are there relationship?
Well, it looks like you *have* solved the security issue. Which "grant" statement did you use?

For the NullPointerException: Paste in both the exception trace (it should show the line number) and the code that triggered it, if you can find it. The NullPointerException should tell where the exception is happening.

CommPortIdentifier.getPortIdentifiers has to be static, since you are not "newing" an instance of that class.

However, portList is a bit different.

Are you defining this SessionBean as stateless or stateful?

Author

Commented:
John,
When I add this:

grant {
        permission java.io.FilePermission "<>", "read,write,delete";
    };

at the end of the file. It *may* be just

    grant {
        permission java.io.FilePermission "read,write,delete";
    };

I got error in java console:
java.security.Policy: error adding Permission java.io.FilePermission java.lang.I
llegalArgumentException: invalid actions mask

I found this:
http://forum.java.sun.com/thread.jsp?forum=63&thread=132580&tstart=0&trange=15

But I got this error again:
Caught java.lang.NullPointerException: name can't be null while loading driver c
om.sun.comm.Win32Driver
Win32Driver Init Exception Ignored
java.security.AccessControlException: access denied (java.io.FilePermission C:\J
DK1.3.1_02\jre\lib\javax.comm.properties delete)
Which one did you put at the end of server.policy? The grant with the <> or the grant without?

Author

Commented:
here is my java.policy
**********************************************
// Standard extensions get all permissions by default

grant codeBase "file:${java.home}/lib/ext/*" {
      permission java.security.AllPermission;
};

//grant {
//    permission java.io.FilePermission ""<<ALL FILES>>", "read,write,delete";
//};

grant {
    permission javax.comm.FilePermission ""<<ALL FILES>>", "read,write,delete";
};


// default permissions granted to all domains


grant {
      // Allows any thread to stop itself using the java.lang.Thread.stop()
      // method that takes no argument.
      // Note that this permission is granted by default only to remain
      // backwards compatible.
      // It is strongly recommended that you either remove this permission
      // from this policy file or further restrict it to code sources
      // that you specify, because Thread.stop() is potentially unsafe.
      // See "http://java.sun.com/notes" for more information.
      permission java.lang.RuntimePermission "stopThread";

      // allows anyone to listen on un-privileged ports
      permission java.net.SocketPermission "localhost:1024-", "listen";

      // "standard" properies that can be read by anyone

      permission java.util.PropertyPermission "java.version", "read";
      permission java.util.PropertyPermission "java.vendor", "read";
      permission java.util.PropertyPermission "java.vendor.url", "read";
      permission java.util.PropertyPermission "java.class.version", "read";
      permission java.util.PropertyPermission "os.name", "read";
      permission java.util.PropertyPermission "os.version", "read";
      permission java.util.PropertyPermission "os.arch", "read";
      permission java.util.PropertyPermission "file.separator", "read";
      permission java.util.PropertyPermission "path.separator", "read";
      permission java.util.PropertyPermission "line.separator", "read";

      permission java.util.PropertyPermission "java.specification.version", "read";
      permission java.util.PropertyPermission "java.specification.vendor", "read";
      permission java.util.PropertyPermission "java.specification.name", "read";

      permission java.util.PropertyPermission "java.vm.specification.version", "read";
      permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
      permission java.util.PropertyPermission "java.vm.specification.name", "read";
      permission java.util.PropertyPermission "java.vm.version", "read";
      permission java.util.PropertyPermission "java.vm.vendor", "read";
      permission java.util.PropertyPermission "java.vm.name", "read";
      permission java.security.AllPermission;
      permission javax.comm.AllPermission;
};

**********************************************

and here is my server.policy:
// Standard extensions get all permissions by default

grant codeBase "file:${java.home}/lib/ext/-" {
    permission java.security.AllPermission;
};

grant codeBase "file:${java.home}/../lib/tools.jar" {
    permission java.security.AllPermission;
};

grant codeBase "file:${com.sun.enterprise.home}/lib/-" {
    permission java.security.AllPermission;
};

grant codeBase "file:${jms.home}/classes/" {
    permission java.security.AllPermission;
};

// Drivers and other system classes should be stored in this
// code base.
grant codeBase "file:${com.sun.enterprise.home}/lib/system/-" {
    permission java.security.AllPermission;
};

// additional permissions for EJBs
grant codeBase "file:${com.sun.enterprise.home}/ejb_impls/-" {
    permission java.lang.RuntimePermission "queuePrintJob";
    permission java.io.FilePermission "${com.sun.enterprise.home}${/}repository${/}-", "read";
};

// additional permissions for servlets
grant codeBase "file:${com.sun.enterprise.home}/public_html/-" {
    permission java.lang.RuntimePermission "loadLibrary.*";
    permission java.lang.RuntimePermission "accessClassInPackage.*";
    permission java.lang.RuntimePermission "queuePrintJob";
    permission java.lang.RuntimePermission "modifyThreadGroup";
    permission java.io.FilePermission "<<ALL FILES>>", "read,write";
};

// additional permissions for standalone resource adapters
grant codeBase "file:${com.sun.enterprise.home}/connector/adapters/-" {
    permission javax.security.auth.PrivateCredentialPermission "* * \"*\"", "read";
    permission java.io.FilePermission "${com.sun.enterprise.home}${/}logs${/}-", "read,write";
};

// permissions for other classes
grant codeBase "file:${com.sun.enterprise.home}/repository/-" {
    permission java.lang.RuntimePermission "loadLibrary.*";
    permission java.lang.RuntimePermission "accessClassInPackage.*";
    permission java.lang.RuntimePermission "queuePrintJob";
    permission java.lang.RuntimePermission "modifyThreadGroup";
    permission java.io.FilePermission "<<ALL FILES>>", "read,write";
    permission javax.security.auth.PrivateCredentialPermission "* * \"*\"", "read";
};


// permissions for default domain
grant {
    permission java.net.SocketPermission "*", "connect";
    permission java.util.PropertyPermission "*", "read";    

    // workaround missing doPrivileged blocks in javamail
    permission java.io.FilePermission "${com.sun.enterprise.home}${/}lib${/}j2ee.jar", "read";

    permission javax.security.auth.PrivateCredentialPermission "javax.resource.spi.security.PasswordCredential * \"*\"", "read";
    permission javax.security.auth.PrivateCredentialPermission "javax.resource.spi.security.GenericCredential * \"*\"", "read";
    permission java.io.FilePermission "read,write,delete";

};
   

and it is the error whenever I execute my code:
java.security.AccessControlException: access denied (java.io.FilePermission C:\J
DK1.3.1_02\jre\lib\javax.comm.properties delete)

and it is the error message when I start up the server:
java.security.Policy: error adding Permission java.io.FilePermission java.lang.I
llegalArgumentException: invalid actions mask
OK -- At the very, very end, remove

permission java.io.FilePermission "read,write,delete";

from // permissions for default domain

(Did you add that or was it there originally?)

Author

Commented:
Ok, I have followed your instruction and add code below

**********************************************
String drivername = "com.sun.comm.Win32Driver";
      try
      {
          CommDriver driver = (CommDriver)
            Class.forName(drivername).newInstance();
            driver.initialize();
      }
      catch (Exception e)
            {
            //just do nothing, it doesn't really matter
            }
**********************************************
and the error at the moment is:
java.lang.NullPointerException: name can't be null


Veronika,

Do you have the lin enumber where the NullPointerException is happening?

Author

Commented:
I never pass :
   CommDriver driver = (CommDriver)
          Class.forName(drivername).newInstance();
          driver.initialize();

even if I delete the java.io package and all its method.

     

Author

Commented:
the error is in the:
OutputStream.write (....)

I have added
grant {
        permission java.io.FilePermission "<>", "read,write,delete";
    };


using policytool

first: in the server.policy but no effect
second: in the java.policy but no effect too
I have restart my j2ee -server first.

have u another suggestion?

Author

Commented:
Finally I can solved the problem.

I don't know if it is combination of all the answer I got.
Last thing I do is move javax.comm.properties to <java_home>\jre\lib from <java_home>\jre\lib\ext

and I move win32com.dll to winnt\system32

Also I add permission.java.io.FilePermission and
permission.java.security.AllPermission

to java.policy.

I add to server.policy before, but no effect.

anyway thank you Jhon.

Regards
Veronika

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.