KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > install > ConnectTest


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.install;
15
16 import java.io.*;
17 import java.net.*;
18 import java.util.*;
19
20 import javax.naming.*;
21 //import javax.management.ObjectName;
22

23 import javax.rmi.PortableRemoteObject JavaDoc;
24 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
25
26 import org.apache.log4j.*;
27
28 import org.compiere.interfaces.*;
29
30 /**
31  * Connection Test
32  *
33  * @author Jorg Janke
34  * @version $Id: ConnectTest.java,v 1.5 2003/10/10 00:59:46 jjanke Exp $
35  */

36 public class ConnectTest
37 {
38     /**
39      * Connection Test Constructor
40      * @param serverName server name or IP
41      */

42     public ConnectTest (String JavaDoc serverName)
43     {
44         System.out.println("ConnectTest: " + serverName);
45         System.out.println();
46         Logger.getRootLogger().setLevel(Level.ALL);
47         //
48
Hashtable env = new Hashtable();
49         env.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
50         env.put(InitialContext.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
51         env.put(InitialContext.PROVIDER_URL, serverName);
52     // env.put(InitialContext.SECURITY_PROTOCOL, ""); // "ssl"
53
// env.put(InitialContext.SECURITY_AUTHENTICATION, "none"); // "none", "simple", "strong"
54
// env.put(InitialContext.SECURITY_PRINCIPAL, "");
55
// env.put(InitialContext.SECURITY_CREDENTIALS, "");
56

57         // Get Context
58
System.out.println ("Creating context ...");
59         System.out.println (" " + env);
60         InitialContext context = null;
61         try
62         {
63             context = new InitialContext(env);
64         }
65         catch (Exception JavaDoc e)
66         {
67             System.err.println("ERROR: Could not create context: " + e);
68             return;
69         }
70
71         testJNP (serverName, context);
72         testEJB (serverName, context);
73
74     } // ConnectTest
75

76     /**
77      * Test JNP
78      * @param serverName server name
79      * @param context context
80      */

81     private void testJNP (String JavaDoc serverName, InitialContext context)
82     {
83         // Connect to MBean
84
System.out.println();
85         System.out.println ("Connecting to MBean ...");
86         try
87         {
88             String JavaDoc connectorName = "jmx:" + serverName + ":rmi";
89             RMIAdaptor server = (RMIAdaptor) context.lookup (connectorName);
90             System.out.println("- have Server");
91             System.out.println("- Default Domain=" + server.getDefaultDomain());
92             System.out.println("- MBeanCount = " + server.getMBeanCount());
93
94     // ObjectName serviceName = new ObjectName ("Compiere:service=CompiereCtrl");
95
// System.out.println("- " + serviceName + " is registered=" + server.isRegistered(serviceName));
96

97     // System.out.println(" - CompiereSummary= "
98
// + server.getAttribute(serviceName, "CompiereSummary"));
99

100             Object JavaDoc[] params = {};
101             String JavaDoc[] signature = {};
102         }
103         catch (Exception JavaDoc e)
104         {
105             System.err.println("ERROR: Could not contact MBean: " + e);
106             return;
107         }
108
109         // List Context
110
System.out.println();
111         System.out.println(" Examining context ....");
112         try
113         {
114             System.out.println(" Namespace=" + context.getNameInNamespace());
115             System.out.println(" Environment=" + context.getEnvironment());
116             System.out.println(" Context '/':");
117             NamingEnumeration ne = context.list("/");
118             while (ne.hasMore())
119                 System.out.println(" - " + ne.nextElement());
120             //
121
System.out.println(" Context 'ejb':");
122             ne = context.list("ejb");
123             while (ne.hasMore())
124                 System.out.println(" - " + ne.nextElement());
125             //
126
System.out.println(" Context 'ejb/compiere':");
127             ne = context.list("ejb/compiere");
128             while (ne.hasMore())
129                 System.out.println(" - " + ne.nextElement());
130         }
131         catch (Exception JavaDoc e)
132         {
133             System.err.println("ERROR: Could not examine context: " + e);
134             return;
135         }
136     } // testJNP
137

138     /**
139      * Test EJB
140      * @param serverName server name
141      * @param context context
142      */

143     private void testEJB (String JavaDoc serverName, InitialContext context)
144     {
145         System.out.println();
146         System.out.println ("Connecting to EJB server ...");
147         try
148         {
149             System.out.println(" Name=" + StatusHome.JNDI_NAME);
150             StatusHome staHome = (StatusHome)context.lookup (StatusHome.JNDI_NAME);
151             System.out.println(" .. home created");
152             Status sta = staHome.create();
153             System.out.println(" .. bean created");
154             System.out.println(" ServerVersion=" + sta.getMainVersion() + " " + sta.getDateVersion());
155             sta.remove();
156             System.out.println(" .. bean removed");
157         }
158         catch (Exception JavaDoc e)
159         {
160             System.err.println("ERROR: Could not connect: " + e);
161             return;
162         }
163
164         System.out.println();
165         System.out.println("SUCCESS !!");
166     } // testEJB
167

168
169     /*************************************************************************/
170
171     /**
172      * Start Method
173      * @param args serverName
174      */

175     public static void main(String JavaDoc[] args)
176     {
177         String JavaDoc serverName = null;
178         if (args.length > 0)
179             serverName = args[0];
180         if (serverName == null || serverName.length() == 0)
181         {
182             try
183             {
184                 serverName = InetAddress.getLocalHost().getHostName();
185             }
186             catch (UnknownHostException ex)
187             {
188                 ex.printStackTrace();
189             }
190         }
191
192         // Log Init
193
LogManager.resetConfiguration();
194         Logger root = Logger.getRootLogger();
195         root.addAppender(new ConsoleAppender(new PatternLayout("%d{ABSOLUTE} %-5p [%c{1}] %m%n")));
196
197         // Start
198
ConnectTest ct = new ConnectTest (serverName);
199     } // main
200

201 } // ConnectionTest
Popular Tags