Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Serial Port Communication using EJB

Posted on 2004-04-12
20
Medium Priority
?
1,642 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      
0
Comment
Question by:VRGultom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 9
20 Comments
 
LVL 4

Accepted Solution

by:
john-at-7fff earned 375 total points
ID: 10820008
Veronika,

That's a pretty neat bean you're writing, but the EJB spec says that you shouldn't be using classes from java.io (e.g., your Streams).

It is entirely possible that your server has its permissions set to keep you from using such classes. Typically, you would need to search for a .policy file for your server or JRE, and explicitly permit the operations you're interested in.

For some background, see

http://www.javaworld.com/javaworld/jw-08-2000/jw-0825-ejbrestrict.html

(particularly item 4.)
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10829859
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
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829907
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."
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10829924
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.
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10830027
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



0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10830069
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";
    };
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10830782
Jhon,

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

Author Comment

by:VRGultom
ID: 10831041
Should I declare the portlist in static?

portList = CommPortIdentifier.getPortIdentifiers();

How about commportidentifier as static method?

what is the relationship?
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10831042
Should I declare the portlist in static?

portList = CommPortIdentifier.getPortIdentifiers();

How about commportidentifier as static method?

are there relationship?
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10832497
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.

0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10833383
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?

0
 
LVL 4

Author Comment

by:VRGultom
ID: 10839055
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)
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10839292
Which one did you put at the end of server.policy? The grant with the <> or the grant without?
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10839992
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
0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10841204
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?)
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10858844
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


0
 
LVL 4

Expert Comment

by:john-at-7fff
ID: 10858949
Veronika,

Do you have the lin enumber where the NullPointerException is happening?
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10859125
I never pass :
   CommDriver driver = (CommDriver)
          Class.forName(drivername).newInstance();
          driver.initialize();

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

     
0
 
LVL 4

Author Comment

by:VRGultom
ID: 10956209
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?
0
 
LVL 4

Author Comment

by:VRGultom
ID: 11047670
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

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the evolution of technology, we have finally reached a point where it is possible to have home automation features like having your thermostat turn up and door lock itself when you leave, as well as a complete home security system. This is a st…
Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question