KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > naming > test > NamingServerSetup


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.test.naming.test;
23
24 import java.util.Hashtable JavaDoc;
25 import javax.naming.RefAddr JavaDoc;
26 import javax.naming.StringRefAddr JavaDoc;
27 import javax.naming.Reference JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.spi.InitialContextFactoryBuilder JavaDoc;
32 import javax.naming.spi.InitialContextFactory JavaDoc;
33 import javax.naming.spi.NamingManager JavaDoc;
34
35 import junit.extensions.TestSetup;
36 import junit.framework.Test;
37
38 import org.jboss.naming.ENCFactory;
39 import org.jnp.server.Main;
40 import org.jnp.interfaces.NamingContext;
41
42 /** Create a naming server instance in setUp and destroy it in tearDown.
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 37406 $
45  */

46 public class NamingServerSetup extends TestSetup
47    implements InitialContextFactoryBuilder JavaDoc
48 {
49    private Main namingServer;
50
51    public NamingServerSetup(Test test)
52    {
53       super(test);
54    }
55
56    public InitialContextFactory JavaDoc createInitialContextFactory(Hashtable JavaDoc environment)
57       throws NamingException JavaDoc
58    {
59       return new InVMInitialContextFactory();
60    }
61
62    protected void setUp() throws Exception JavaDoc
63    {
64       super.setUp();
65       namingServer = new Main();
66       namingServer.setPort(10099);
67       namingServer.start();
68
69       NamingManager.setInitialContextFactoryBuilder(this);
70       /* Bind an ObjectFactory to "java:comp" so that "java:comp/env" lookups
71          produce a unique context for each thread contexxt ClassLoader that
72          performs the lookup.
73       */

74       InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
75       ClassLoader JavaDoc topLoader = Thread.currentThread().getContextClassLoader();
76       ENCFactory.setTopClassLoader(topLoader);
77       RefAddr JavaDoc refAddr = new StringRefAddr JavaDoc("nns", "ENC");
78       Reference JavaDoc envRef = new Reference JavaDoc("javax.naming.Context", refAddr, ENCFactory.class.getName(), null);
79       Context JavaDoc ctx = (Context JavaDoc)iniCtx.lookup("java:");
80       ctx.rebind("comp", envRef);
81    }
82
83    protected void tearDown() throws Exception JavaDoc
84    {
85       namingServer.stop();
86       super.tearDown();
87    }
88
89    static class InVMInitialContextFactory implements InitialContextFactory JavaDoc
90    {
91       public Context JavaDoc getInitialContext(Hashtable JavaDoc env)
92          throws NamingException JavaDoc
93       {
94          Hashtable JavaDoc env2 = (Hashtable JavaDoc) env.clone();
95          env2.remove(Context.PROVIDER_URL);
96          return new NamingContext(env2, null, null);
97       }
98    }
99 }
100
Popular Tags