KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > naming > NamingAlias


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.naming;
23
24 import javax.naming.NamingException JavaDoc;
25
26 import org.jboss.system.ServiceMBean;
27 import org.jboss.system.ServiceMBeanSupport;
28
29 /**
30  * A simple utility mbean that allows one to create an alias in
31  * the form of a LinkRef from one JNDI name to another.
32  *
33  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
34  *
35  * @author <a HREF="mailto:Scott_Stark@displayscape.com">Scott Stark</a>.
36  * @version $Revision: 38211 $
37  */

38 public class NamingAlias
39    extends ServiceMBeanSupport
40    implements NamingAliasMBean
41 {
42    private String JavaDoc fromName;
43    private String JavaDoc toName;
44
45    public NamingAlias()
46    {
47       this(null, null);
48    }
49    
50    public NamingAlias(final String JavaDoc fromName, final String JavaDoc toName)
51    {
52       this.fromName = fromName;
53       this.toName = toName;
54    }
55    
56    /**
57     * Get the from name of the alias. This is the location where the
58     * LinkRef is bound under JNDI.
59     *
60     * @jmx:managed-attribute
61     *
62     * @return the location of the LinkRef
63     */

64    public String JavaDoc getFromName()
65    {
66       return fromName;
67    }
68
69    /**
70     * Set the from name of the alias. This is the location where the
71     * LinkRef is bound under JNDI.
72     *
73     * @jmx:managed-attribute
74     *
75     * @param name, the location where the LinkRef will be bound
76     */

77    public void setFromName(String JavaDoc name) throws NamingException JavaDoc
78    {
79       removeLinkRef(fromName);
80       this.fromName = name;
81       createLinkRef();
82    }
83
84    /**
85     * Get the to name of the alias. This is the target name to
86     * which the LinkRef refers. The name is a URL, or a name to be resolved
87     * relative to the initial context, or if the first character of the name
88     * is ".", the name is relative to the context in which the link is bound.
89     *
90     * @jmx:managed-attribute
91     *
92     * @return the target JNDI name of the alias.
93     */

94    public String JavaDoc getToName()
95    {
96       return toName;
97    }
98
99    /**
100     * Set the to name of the alias. This is the target name to
101     * which the LinkRef refers. The name is a URL, or a name to be resolved
102     * relative to the initial context, or if the first character of the name
103     * is ".", the name is relative to the context in which the link is bound.
104     *
105     * @jmx:managed-attribute
106     *
107     * @param name, the target JNDI name of the alias.
108     */

109    public void setToName(String JavaDoc name) throws NamingException JavaDoc
110    {
111       this.toName = name;
112       
113       createLinkRef();
114    }
115    
116    protected void startService() throws Exception JavaDoc
117    {
118       if( fromName == null )
119          throw new IllegalStateException JavaDoc("fromName is null");
120       if( toName == null )
121          throw new IllegalStateException JavaDoc("toName is null");
122       createLinkRef();
123    }
124    
125    protected void stopService() throws Exception JavaDoc
126    {
127       removeLinkRef(fromName);
128    }
129    
130    private void createLinkRef() throws NamingException JavaDoc
131    {
132       if( super.getState() == ServiceMBean.STARTING || super.getState() == ServiceMBean.STARTED )
133          Util.createLinkRef(fromName, toName);
134    }
135    
136    /**
137     * Unbind the name value if we are in the STARTED state.
138     */

139    private void removeLinkRef(String JavaDoc name) throws NamingException JavaDoc
140    {
141       if(super.getState() == ServiceMBean.STOPPING)
142          Util.removeLinkRef(name);
143    }
144 }
145
Popular Tags