KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > AppContainer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.appclient;
24
25 import javax.naming.InitialContext JavaDoc;
26 import javax.security.auth.callback.CallbackHandler JavaDoc;
27 import javax.security.auth.login.LoginContext JavaDoc;
28 import javax.security.auth.login.LoginException JavaDoc;
29 import com.sun.enterprise.Switch;
30 import com.sun.enterprise.NamingManager;
31 import com.sun.enterprise.InvocationManager;
32 import com.sun.enterprise.InjectionManager;
33 import com.sun.enterprise.util.InjectionManagerImpl;
34 import com.sun.enterprise.ComponentInvocation;
35 import com.sun.enterprise.J2EETransactionManager;
36 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
37 import com.sun.enterprise.naming.NamingManagerImpl;
38 import com.sun.enterprise.util.InvocationManagerImpl;
39 import com.sun.enterprise.distributedtx.J2EETransactionManagerImpl;
40 import com.sun.enterprise.security.auth.LoginContextDriver;
41 import com.sun.enterprise.iiop.PEORBConfigurator;
42
43 import java.util.logging.Logger JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import com.sun.logging.LogDomains;
46
47 import java.util.Properties JavaDoc;
48 /**
49  * This is the application client container. It performs tasks such as
50  * user login and other initialization based on the deployment descriptor
51  * information.
52  * @author Vivek Nagar
53  * @author Harpreet Singh
54  */

55 public class AppContainer
56 {
57     public static final int USERNAME_PASSWORD = 1;
58     public static final int CERTIFICATE = 2;
59     // harry - added for LoginContextDriver access
60
public static final int ALL = 3;
61     private static final boolean debug = false;
62
63     private ApplicationClientDescriptor descriptor = null;
64     private Switch sw = null;
65     private NamingManager nm = null;
66
67     // the handler should be defined - it will be used by the J2EEKeyManager
68
// to pick up the certificate alias when performing SSL handshake during
69
// lazy authentication.
70
private static CallbackHandler JavaDoc handler = null;
71     private boolean guiAuth = true;
72
73     private static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.ACC_LOGGER);
74
75     /**
76      * The constructor takes the application client descriptor as an argument.
77      */

78     public AppContainer(ApplicationClientDescriptor desc, boolean useGuiAuth)
79     {
80     sw = Switch.getSwitch();
81     sw.setContainerType(Switch.APPCLIENT_CONTAINER);
82         // Inform the JTS that this is a APPCLIENT_CONTAINER . This is required
83
// as JTS can not use Switch interface as it creates a circular dependency
84
// Configuration.setAsAppClientConatiner();
85
descriptor = desc;
86         guiAuth = useGuiAuth;
87     }
88
89     /**
90      * This returns the CallbackHandler for the AppContainer.
91      * @return handler
92      */

93     public static CallbackHandler JavaDoc getCallbackHandler() {
94         return handler;
95     }
96
97     /**
98      * This is called by main before the actual main of the application
99      * is invoked. It initializes the container and performs login for
100      * the user.
101      * @return the main class of the application.
102      */

103     public String JavaDoc preInvoke(Properties JavaDoc props) throws Exception JavaDoc
104     {
105         InitialContext JavaDoc ic = new InitialContext JavaDoc(props);
106
107     ComponentInvocation ci = new ComponentInvocation(null, this);
108
109     InvocationManager im = new InvocationManagerImpl();
110     sw.setInvocationManager(im);
111
112     // Initialize Transaction service. By now the ORB must have
113
// been created during the new InitialContext() call using
114
// the ORBManager
115
PEORBConfigurator.initTransactionService(null, new Properties JavaDoc() );
116     
117     /* Create the transaction manager and set it in the switch. */
118     J2EETransactionManager tm = new J2EETransactionManagerImpl();
119     sw.setTransactionManager(tm);
120
121     im.preInvoke(ci);
122     
123     // Do security setup
124
String JavaDoc callbackHandler = descriptor.getCallbackHandler();
125         
126     _logger.fine("Callback Handler:" + callbackHandler);
127         
128     initializeCallbackHandler(callbackHandler);
129     
130     /* Login the user. */
131     // eager authentication!
132
boolean doLogin =
133         Boolean.valueOf(System.getProperty("startup.login", "false")).booleanValue();
134     if(doLogin) {
135             _logger.info("acc.init_login");
136         String JavaDoc loginMech = System.getProperty(
137         "com.sun.enterprise.loginMech", "password");
138         if(loginMech.equalsIgnoreCase("ssl")) {
139         LoginContextDriver.doClientLogin(CERTIFICATE, handler);
140         } else if(loginMech.equalsIgnoreCase("all")) {
141         LoginContextDriver.doClientLogin(ALL, handler);
142         } else {
143         LoginContextDriver.doClientLogin(USERNAME_PASSWORD, handler);
144         }
145     }
146
147     /* Create the naming manager and set it in the switch. */
148     nm = new NamingManagerImpl(ic);
149     sw.setNamingManager(nm);
150
151     String JavaDoc mainClass = descriptor.getMainClassName();
152     sw.setDescriptorFor(this, descriptor);
153     nm.bindObjects(descriptor);
154
155         // Create the Injection Manager and set it on the switch.
156
InjectionManager injectionMgr = new InjectionManagerImpl();
157         sw.setInjectionManager(injectionMgr);
158
159     return mainClass;
160     }
161
162     /**
163      * This is called by the main after the real main of the application
164      * is invoked. Performs cleanup.
165      */

166     public void postInvoke() throws Exception JavaDoc
167     {
168     nm.unbindObjects(descriptor);
169     }
170
171
172     /**
173      * Initialize the JAAS login modules.
174      * One login module per realm (default and certificate).
175      * @param the JAAS callback handler class.
176      * @exception LoginException if there was an error.
177      */

178     private void initializeCallbackHandler(String JavaDoc callbackHandler){
179     Class JavaDoc handlerClass = null;
180     handler = null;
181     
182     try {
183         if(callbackHandler != null) {
184             handlerClass = Class.forName(callbackHandler);
185         handler = (CallbackHandler JavaDoc) handlerClass.newInstance();
186         } else {
187             handler = new com.sun.enterprise.security.auth.login.LoginCallbackHandler(guiAuth);
188         }
189     } catch(Exception JavaDoc e) {
190             _logger.log(Level.FINE, "Could not instantiate specified " +
191                          "callback handler:" + e.getMessage(), e);
192             _logger.info("acc.using_default_callback");
193         handler = new com.sun.enterprise.security.auth.login.LoginCallbackHandler(guiAuth);
194     }
195     }
196 }
197
198
Popular Tags