participate


Sun Java System Application Server Standard and Enterprise Edition - Changing env-entry after deployment
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
Steven Williams
Posts:5
Registered: 9/8/03
Changing env-entry after deployment   
Sep 8, 2003 7:17 PM

 
Hi,

I was wondering how I can change the value of an env-entry after deployment? Do I have to change the value in the ejb-jar.xml, redeploy the application, or is there a utility I can use?

thanks
Steve
 
sanjeevsaha
Posts:1
Registered: 1/6/00
Re: Changing env-entry after deployment   
Oct 14, 2005 12:13 AM (reply 1 of 5)  (In reply to original post )

 
Hello Steven:

Did you by any chance happen to get an answer to your question?

-Sanjeev
 
bradpeabody
Posts:5
Registered: 1/24/06
Re: Changing env-entry after deployment   
Jan 25, 2006 9:14 AM (reply 2 of 5)  (In reply to #1 )

 
Yeah - I'd like to know too - what's the point of defining it as an environment entry if the system administrator/deployer/whatever (as opposed to the developer) can't change the value?
 
bradpeabody
Posts:5
Registered: 1/24/06
Re: Changing env-entry after deployment   
Jan 25, 2006 2:56 PM (reply 3 of 5)  (In reply to #2 )

 
Okay gentlemen, I dug around and couldn't find anything on this concept of defining an env-entry after deployment using Sun Java System Application Server 8.1 - so I just came up with my own hack. Thought I'd share it in case others have the same question and can't find the answer.

Just to restate the problem: In SJAS you can't seem to define an environment entry that you can just look up via JNDI (you can do this in Tomcat pretty easy though using the context.xml file and an env-entry like syntax) - the benefit of this being of course that you can easily define deployment-time variables which a system administrator can set based on his setup. There seems to be no way to do this is SJAS 8.1

As a workaround, you can create your own custom JNDI resource which will expose the properties that you set in the Admin Console as a simple java.util.Map that you can use from your app.

Here's the blow by blow:

2. Create a JNDIProperties object that implements java.util.Map but does NOT implement java.io.Serializable:

package helpers.jndi;
 
import java.util.Collection;
import java.util.Map;
import java.util.Set;
 
public final class JNDIProperties implements Map {
    
    private Map map;
    
    public JNDIProperties(Map aMap) { map = aMap; }
 
    public boolean containsKey(Object key) { return map.containsKey(key); }
 
    public void clear() { map.clear(); }
 
    public Object get(Object key) { return map.get(key); }
 
    public boolean containsValue(Object value) { return map.containsValue(value); }
 
    public Set entrySet() { return map.entrySet(); }
 
    public boolean isEmpty() { return map.isEmpty(); }
 
    public Set keySet() { return map.keySet(); }
 
    public Object put(Object key, Object value) { return map.put(key, value); }
 
    public void putAll(Map t) { map.putAll(t); }
 
    public Object remove(Object key) { return map.remove(key); }
 
    public int size() { return map.size(); }
 
    public Collection values() { return map.values(); }
    
}


2. Create an object that implements javax.naming.spi.ObjectFactory and returns an instance of JNDIProperties containing the properties defined in the container:

package helpers.jndi;
 
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
 
public class PropertyFactory implements ObjectFactory {
    
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
        Map myMap = new Hashtable();
        Reference myReference = (Reference)obj;
        Enumeration myEnumeration = myReference.getAll();
        while (myEnumeration.hasMoreElements()) {
            RefAddr myRefAddr = (RefAddr)myEnumeration.nextElement();
            myMap.put(myRefAddr.getType(), myRefAddr.getContent());
        }
        JNDIProperties myJNDIProperties = new JNDIProperties(myMap);
        return myJNDIProperties;
    }
    
}


3. Build this and copy the jar to $SJAS_HOME/domains/domain1/lib/ext/ (or other appropriate folder for your setup), then restart the app server to get to load the jar.

4. Go to the admin console and go to Resources -> JNDI -> Custom Resources -> New. Give it a sensible JNDI name (I'll use "testproperties" in this example), enter java.util.Map as the resource type and the name of your factory class for the "factory class" field, e.g. "helpers.jndi.PropertyFactory". Below that under "Additional Properties", add in each of the properties that you want to expose, for example enter a property named "testproperty1" with the value of "my test".

5. You can now get a java.util.Map that contains the properties that you set for your custom JNDI resource by doing something like the following (example in JSP):

<%
Context myContext = new InitialContext();
Context myEnvContext = (Context)myContext.lookup("java:comp/env");
Map myMap = (Map)myEnvContext.lookup("testproperties");
%>
<%=myMap.get("testproperty1")%>


Output is:

my test


NOTE: If this sounds convoluted - I agree, it is. If anyone's got a better idea, please post it; as I could not find another solution to this seemingly simple problem.
 
bradpeabody
Posts:5
Registered: 1/24/06
Re: Changing env-entry after deployment   
Jan 30, 2006 1:53 AM (reply 4 of 5)  (In reply to #3 )

 
Sorry, I forgot and important step, you also have to add the appropriate data to your web.xml and sun-web.xml (you can do this in Netbeans using the GUI by going to web.xml -> Resources -> Resource References -> Add - etc.


web.xml:

    <resource-ref>
        <res-ref-name>testproperties</res-ref-name>
        <res-type>java.util.Map</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

sun-web.xml:

  <resource-ref>
    <res-ref-name>testproperties</res-ref-name>
    <jndi-name>testproperties</jndi-name>
  </resource-ref>
 
SunnyYoung
Posts:1
Registered: 3/27/07
Re: Changing env-entry after deployment   
Mar 27, 2007 8:47 AM (reply 5 of 5)  (In reply to #3 )

 
Hi,
I did exactly what you post here with web.xml and sun-web.xml setting. But it didn't run PropertyFactory at all and it returns me a Reference type in the program like
<%
Context myContext = new InitialContext();
Context myEnvContext = (Context)myContext.lookup("java:comp/env");
Reference myMap = (Reference)myEnvContext.lookup("testproperties");
%>

Does anyone know what's wrong with it???
Thank you
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums