Hi,

I try to adapt that MBean example in JBoss doc wich puts a HashMap in the
InitialContext. Now I try from another class (a struts ActionClass) to
access that HashMap the Object I get is null. Here is the code wich I use to
access that HashMap:

java.util.Properties p = System.getProperties();
                p.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
                p.put(javax.naming.Context.PROVIDER_URL, "localhost");
                HashMap map = null;
                try {
                        javax.naming.InitialContext jndiContext = new
javax.naming.InitialContext(p);
                        Context subContext = (Context)jndiContext.lookup("mt");
                        Object obj = subContext.lookup("MetaObjectFactory");
                        map = (HashMap) obj;
                } catch(NamingException e) {
                        log.error("NamingException", e);
                }

The object subContext contains correctly mt but it returns null for the name
MetaObjectFactory.

Here is the MBean that I use:

public class MetaObjectReader
        extends ServiceMBeanSupport
        implements MetaObjectReaderMBean {

        private String configurationFile;
        private String jndiName;

        /**
         * @see ServiceMBean#getName()
         */
        public String getName() {
                return "DynamicObjectModelFactoryMBean";
        }

        /**
         * @see DynamicObjectModelFactoryMBeanI#setConfigurationFile(String)
         */
        public void setConfigurationFile(String fileName) throws NamingException {
                this.configurationFile = fileName;
        }

        /**
         * @see DynamicObjectModelFactoryMBeanI#getConfigurationFile()
         */
        public String getConfigurationFile() {
                return this.configurationFile;
        }

        /**
         * @see DynamicObjectModelFactoryMBeanI#setJNDIName(String)
         */
        public void setJndiName(String name) throws NamingException {
                String oldName = this.jndiName;
                this.jndiName = name;
                if (super.getState() == STARTED) {
                        unbind(oldName);
                        try {
                                rebind();
                        } catch (Exception e) {
                                NamingException ne = new NamingException("Failed to 
update jndiName");
                                ne.setRootCause(e);
                                throw ne;
                        }
                }
        }

        /**
         * @see DynamicObjectModelFactoryMBeanI#getJNDIName()
         */
        public String getJndiName() {
                return jndiName;
        }

        public void startService() throws Exception {
                readConfiguration();
                rebind();
        }

        public void stopService() {
                unbind(jndiName);
        }

        private void readConfiguration() {
        }

        private static Context createContext(Context rootContext, Name name)
                throws NamingException {
                Context subctx = rootContext;
                for (int n = 0; n < name.size(); n++) {
                        String atom = name.get(n);
                        try {
                                Object obj = subctx.lookup(atom);
                                subctx = (Context) obj;
                        } catch (NamingException e) {
                                // No binding exists, create a subcontext
                                subctx = subctx.createSubcontext(atom);
                        }
                }

                return subctx;
        }

        private void rebind() throws NamingException {
                InitialContext rootCtx = new InitialContext();
                // Get the parent context into which we are to bind
                Name fullName = rootCtx.getNameParser("").parse(jndiName);
                log.debug("fullName=" + fullName);
                Name parentName = fullName;
                if (fullName.size() > 1)
                        parentName = fullName.getPrefix(fullName.size() - 1);
                else
                        parentName = new CompositeName();
                Context parentCtx = createContext(rootCtx, parentName);
                Name atomName = fullName.getSuffix(fullName.size() - 1);
                String atom = atomName.get(0);
                // hier die Factory einbinden
                NonSerializableFactory.rebind(parentCtx, atom, new HashMap());
        }

        private void unbind(String jndiName) {
                try {
                        Context rootCtx = (Context) new InitialContext();
                        rootCtx.unbind(jndiName);
                        NonSerializableFactory.unbind(jndiName);
                } catch (NamingException e) {
                        log.error("NamingException", e);
                }
        }
}


MANY Thanks and any suggestions are welcome !


_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to