properties file in a jar for EJB 3.0

R

RVic

I have an EJB 3.0 that I need to parameterize with a properties file.
Where do I put this file in the directory structure within the jar,
and how do I access it? Thanks RVic
 
J

jlp

RVic a écrit :
I have an EJB 3.0 that I need to parameterize with a properties file.
Where do I put this file in the directory structure within the jar,
and how do I access it? Thanks RVic
Look at ClassLoader.getResourceAsStream(String resource).
 
R

RVic

But where does it go in the directory structure? If I have a file
called myproperties.properties, do I put it in /MANIFEST-INF? Do I put
it at the top level dir in the jar?
 
G

Giovanni Azua

Hello RVic,

In short, you shouldn't access files specially not for the purpose of
configuration. This is explicitly discouraged/forbidden in the EJB3-spec
[1].



See the
 
G

Giovanni Azua

Hello RVic,

Please ignore my previous post, by mistake I pressed the wrong key
combination.

RVic said:
I have an EJB 3.0 that I need to parameterize with a properties file.
Where do I put this file in the directory structure within the jar,
and how do I access it? Thanks RVic
In short, you shouldn't access files specially not for the purpose of
configuration. This is explicitly discouraged/forbidden in the EJB3-spec
[1 p545].

"
.. An enterprise bean must not use the java.io package to attempt to access
files and directories in the file system.

The file system APIs are not well-suited for business components to access
data. Business components should use a resource manager API, such as JDBC,
to store data."

You should instead use <env-entry> and get the values injected using
@Resource.

HTH,
Best regards,
Giovanni

[1] JSR-000220 Enterprise JavaBeans 3.0 Final Release (ejbcore)
<http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html>
 
L

Lew

jlp said:
Look at ClassLoader.getResourceAsStream(String resource).

or Class.getResourceAsStream( String resource )
But where does it go in the directory structure? If I have a file
called myproperties.properties, do I put it in /MANIFEST-INF? Do I put
it at the top level dir in the jar?

Talk a look at the Javadocs for getResourceAsStream(). You put the
resource in the classpath, i.e., in the same directory tree where
'.class' files go inside the JAR. An abslolute path in the argument
to getResourceAsStream() corresponds to a path relative to a classpath
entry.

See the rules for path resolution at
<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource
(java.lang.String)>

Bear in mind we are reading from the class path for the application,
i.e., paths inside the JAR, not the file system overall.

JAYF - Javadocs are your friend. Be in the habit of reading them.
 
R

RVic

Giovanni,

Ok, but what file do I find <env-entry> in? Is it in an xml file in
MANIFEST-INF? Or is it in an xml file I put in my deploy directory
(Using JBoss 5.1, ejb 3.0) Thank you so much. RVic
 
G

Giovanni Azua

Hi RVic,

RVic said:
Ok, but what file do I find <env-entry> in? Is it in an xml file in
MANIFEST-INF? Or is it in an xml file I put in my deploy directory
(Using JBoss 5.1, ejb 3.0) Thank you so much. RVic
You place env-entry constants in:
META-INF/ejb-jar.xml

"The ejb-jar file must contain the deployment descriptor (if any) in the
format defined in Chapter 19. The
deployment descriptor must be stored with the name META-INF/ejb-jar.xml in
the ejb-jar file." [1, p540]

See an example of ejb-jar.xml below taken from that same spec [1, p411] and
the configurable attributes declared as [1, p412]:

// The maximum number of tax exemptions, configured by the Deployer.
@Resource int maxExemptions = 4; // defaults to 4

Another interesting reason to avoid reading even from the same ejb jar [1,
p546]:

"The enterprise bean must not attempt to directly read or write a file
descriptor.
Allowing the enterprise bean to read and write file descriptors directly
could compromise security."

HTH,
Best regards,
Giovanni

<?xml version = '1.0' encoding = 'windows-1252'?>
<ejb-jar version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_3_0.xsd">
<enterprise-beans>
<session>
...
<ejb-name>EmployeeService</ejb-name>
<ejb-class>com.wombat.empl.EmployeeServiceBean</ejb-class>
...
<env-entry>
<description>
The maximum number of tax exemptions
allowed to be set.
</description>
<env-entry-name>maxExemptions</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>15</env-entry-value>
</env-entry>
<env-entry>
<description>
The minimum number of tax exemptions
allowed to be set.
</description>
<env-entry-name>minExemptions</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>1</env-entry-value>
</env-entry>
<env-entry>
<env-entry-name>foo/name1</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>value1</env-entry-value>
</env-entry>
<env-entry>
<env-entry-name>foo/bar/name2</env-entry-name>
<env-entry-type>java.lang.Boolean</env-entry-type>
<env-entry-value>true</env-entry-value>
</env-entry>
<env-entry>
<description>Some description.</description>
<env-entry-name>name3</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
<env-entry>
<env-entry-name>foo/name4</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>10</env-entry-value>
</env-entry>
...
</session>
</enterprise-beans>
<interceptors/>
<assembly-descriptor/>
</ejb-jar>
 
R

RVic

Giovanni,

Thank you. I need this not for a session bean, but for an mbean. In
my /META-INF I have presently 2 xml files, a persistence.xml, and a
jboss-service.xml, as shown below. Should I create a seperate xml file
for these parameters? They will be used by the mbean itself that I am
setting up in jbos-service.xml. -RVic

<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="com.s.listener.SocketService"
name="com.s:service=PortBinding">
<depends>jboss.j2ee:jar=s-trans-
server.jar,name=DispatchHandlerBean,service=EJB3</depends>
<attribute name="Message">Port Listening</attribute>
<!-- NOTE: sys admin's should configure listening ports here -->
<attribute name="Port">20001</attribute>
<attribute name="Port7e1">20002</attribute>
</mbean>
</server>
 
G

Giovanni Azua

RVic said:
Thank you. I need this not for a session bean, but for an mbean. In
my /META-INF I have presently 2 xml files, a persistence.xml, and a
jboss-service.xml, as shown below. Should I create a seperate xml file
yes in the ejb-jar.xml
<mbean code="com.s.listener.SocketService"
:))) yet another violation of the ejb spec! [sigh]

"An enterprise bean must not attempt to listen on a socket, accept
connections on a socket, or
use a socket for multicast.
The EJB architecture allows an enterprise bean instance to be a network
socket client, but it does not
allow it to be a network server. Allowing the instance to become a network
server would conflict with
the basic function of the enterprise bean- to serve the EJB clients." [1,
p545]
 
A

Arne Vajhøj

Giovanni said:
RVic said:
I have an EJB 3.0 that I need to parameterize with a properties file.
Where do I put this file in the directory structure within the jar,
and how do I access it? Thanks RVic
In short, you shouldn't access files specially not for the purpose of
configuration. This is explicitly discouraged/forbidden in the EJB3-spec
[1 p545].

"
. An enterprise bean must not use the java.io package to attempt to access
files and directories in the file system.

That does not prohibit reading properties as a resource
within the jar file.

Arne
 
A

Arne Vajhøj

RVic said:
But where does it go in the directory structure? If I have a file
called myproperties.properties, do I put it in /MANIFEST-INF? Do I put
it at the top level dir in the jar?

Anywhere.

You just open it with the path it has in the jar file.

Arne
 
R

RVic

Thanks Guys,

I just realized.....it isn;t getting put into my jar file because I
don;t have that specified in my Ant script. I;ve been operating the
past few hours assuming, because it is in my project's filesystem,
that it was also in the jar. Duh! Yes, now I can access it. Thanks so
much guys.

As for using resource injection, I have a need for that too with the
same bean. However, I am not sure in the ejb-jar.xml how to specify
the onw tag for it:
<enterprise-beans>
<session>
....

Because this is NOT a session bean, it is a message bean. Would I
therefore put this as:
<enterprise-beans>
<message>

? Thanks again.
 
A

Arne Vajhøj

RVic said:
I just realized.....it isn;t getting put into my jar file because I
don;t have that specified in my Ant script. I;ve been operating the
past few hours assuming, because it is in my project's filesystem,
that it was also in the jar. Duh! Yes, now I can access it.

The small details ...

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top