Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

ejb and file I/o

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to ejb and I came to know that usage of the java.io package is forbidden in the spec but in my method implemented in ejb bean i need to write to a external file.And a shell script is then executed which reads this file and does furhter processing.Is there some other way to achive this result ans will there be problems if I use the following code.Any suggestion in this matter is highly apprecaited. Following is the piece of code.. from my method <html> <body>


try
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("c:\\testdata.txt"))));
pw.print("aaaaaa");
pw.print("bbbbbbbbbbbbbbbb");
pw.prnt(name);
pw.print(seqid);
pw.close();
}
catch(IOException e)
{
e.getMessage();
}
and then i am using following piece of code to run shell script from that method in ejb
try
{
String command = "sh /export/home//submitData.sh " + currentfile ;
Process p = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{}

</body> </html> Thanks, peter
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
I think the reason that this is not allowed is so you can't compromise the running of the container by writing huge files to the server.
Perhaps this could be overcome by providing a servlet that writes to a file and calling the servlet from the Enterprise JavaBean.
Cheers,

Originally posted by peter brews:
I am new to ejb and I came to know that usage of the java.io package is forbidden in the spec but in my method implemented in ejb bean i need to write to a external file.And a shell script is then executed which reads this file and does furhter processing.Is there some other way to achive this result ans will there be problems if I use the following code.Any suggestion in this matter is highly apprecaited. Following is the piece of code.. from my method <html> <body>


try
{
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("c:\\testdata.txt"))));
pw.print("aaaaaa");
pw.print("bbbbbbbbbbbbbbbb");
pw.prnt(name);
pw.print(seqid);
pw.close();
}
catch(IOException e)
{
e.getMessage();
}
and then i am using following piece of code to run shell script from that method in ejb
try
{
String command = "sh /export/home//submitData.sh " + currentfile ;
Process p = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{}

</body> </html> Thanks, peter

 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Process p = Runtime.getRuntime().exec(command);


I am sure you are not supposed to invoke the above line of code too.
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, let's get something straight here. It's not "forbidden" by the Spec. That particular restriction is in a part of the spec that says "for maximum portability you should avoid this". However, in fact, every EJB server I know of will allow you to use java.io from within an EJB. Nobody really cares about this... However, I'd still probably wrap my file access into a utility Javabean and call that from my EJB...
Kyle
 
peter brews
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your helpful replies.I code that i had mentioned i in my EJB actually works on my local machine ,but not sure if it will work when multiple users from diffrent systems will call that method.
1. I also wanted to know if the restriction is for security reasons only.
2. Will it be better if i put all this code in seperate java class and call that class in EJB.Does method should be static .If not ,how do i instantiate a class in EJB.
for eg.
I would write a class UtilityMethods.java an will define methods in that say writeData.
Then i will call this method as UtilityMethods.writeData and i will pass the data as string parameter to this method.
3.In most of other mymethods that were defined in EJB i am using url connection to pass and get information .Will there be any problem with that too?
4.Where can i get more details about EJB's.

Thanks,
pete
 
Kyle Brown
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by peter brews:
Thank you all for your helpful replies.I code that i had mentioned i in my EJB actually works on my local machine ,but not sure if it will work when multiple users from diffrent systems will call that method.
1. I also wanted to know if the restriction is for security reasons only.
2. Will it be better if i put all this code in seperate java class and call that class in EJB.Does method should be static .If not ,how do i instantiate a class in EJB.
for eg.
I would write a class UtilityMethods.java an will define methods in that say writeData.
Then i will call this method as UtilityMethods.writeData and i will pass the data as string parameter to this method.
3.In most of other mymethods that were defined in EJB i am using url connection to pass and get information .Will there be any problem with that too?
4.Where can i get more details about EJB's.

Thanks,
pete


The restriction isn't there for security reasons, but more for portability. Some EJB servers might not (theoretically) allow file I/O, but practically all of them do.
Yes, you should move the method into a utility class. However the method doesn't have to be static. You just create an instance of the object like you would any other object. Use the new operator with a constructor for the class.
Now if may I ask -- is this your first exposure to Java? If so RUN AWAY from EJB's at this point! You need to get the basics down before tackling more advanced issues like this.
So, first, read several dozen good Java books and get your Java skills up to speed. Then, if you're ready, read Richard Monson-Haefel's EJB book, or Ed Roman's EJB book -- but only after you've had a few months to master Java!
Kyle
[ June 03, 2002: Message edited by: Kyle Brown ]
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,
Can you show how do you manage to get the file access working?
I've tried to do a file access on a stateless session bean on J2EE-RI and it throws a
java.security.AccessControlException: access denied (java.io.FilePermission f:\temp\test.txt write)
I wonder where can I specify the policy or configuration for the FileAccess for the directory.
Thanks for your advice.
Cheers.

Han Ming
 
peter brews
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i had not visited this site since a couple of weeks.
Definea method in the bean
and try using following code.this works for me.
PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter(new File("mytext.txt"))));

pete
 
reply
    Bookmark Topic Watch Topic
  • New Topic