Coverage details for com.clustercontrol.performance.rrdtool.util.LoginManager

LineHitsSource
1 /*
2  
3 Copyright (C) 2008 NTT DATA Corporation
4  
5 This program is free software; you can redistribute it and/or
6 Modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation, version 2.
8  
9 This program is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the GNU General Public License for more details.
13  
14  */
15  
16 package com.clustercontrol.performance.rrdtool.util;
17  
18 import java.lang.reflect.Method;
19 import java.rmi.AccessException;
20 import java.util.HashMap;
21 import java.util.Hashtable;
22  
23 import javax.naming.CommunicationException;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.security.auth.login.AppConfigurationEntry;
27 import javax.security.auth.login.Configuration;
28 import javax.security.auth.login.LoginContext;
29 import javax.security.auth.login.LoginException;
30  
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.jboss.naming.NamingContextFactory;
34 import org.jboss.security.auth.callback.UsernamePasswordHandler;
35 import org.jnp.interfaces.NamingContext;
36  
37 import com.clustercontrol.accesscontrol.ejb.session.AccessCheck;
38 import com.clustercontrol.accesscontrol.ejb.session.AccessCheckHome;
39 import com.clustercontrol.util.Messages;
40  
41 /**
42  * Hinemos Addon for RRDTool<br>
43  * ログインマネージャクラス<br>
44  *
45  * @since 3.0.0
46  */
471public class LoginManager {
48     public static final String KEY_EJB_URL = "ejbUrl";
49     public static final String VALUE_EJB_URL = "jnp://localhost:1099";
50     public static final String KEY_EJB_UID = "ejbUid";
51     public static final String VALUE_EJB_UID = "hinemos";
52  
53     public static final String USER_OBJECT_ID = "com.clustercontrol.accesscontrol.user";
54     public static final String URL_OBJECT_ID = "com.clustercontrol.accesscontrol.url";
55  
56     public static final String ACTION_SET_ID = "com.clustercontrol.accesscontrol.ActionSet";
57     public static final String ACTION_ID_LOGIN = "com.clustercontrol.accesscontrol.etc.action.LoginAction";
58     public static final String ACTION_ID_LOGOUT = "com.clustercontrol.accesscontrol.etc.action.LogoutAction";
59  
601    private static LoginManager m_instance = null;
611    private NamingContext m_namingcontext = null;
621    private LoginContext m_loginContext = null;
631    private String m_url = null;
641    private String m_uid = null;
651    private String m_password = null;
66  
67     // 実行ログ出力用クラス
681    private static Log log = LogFactory.getLog(LoginManager.class);
69  
70     /**
71      * こ?オブジェクトを取得します?? シングルトン
72      *
73      * @return LoginManager ログインマネージャ
74      *
75      * @version 2.0.0
76      * @since 2.0.0
77      */
78     public static LoginManager getContextManager() {
7918        if (m_instance == null) {
801            m_instance = new LoginManager();
81         }
8218        return m_instance;
83     }
84  
85     /**
86      * コンストラクタ
87      *
88      * @version 2.0.0
89      * @since 2.0.0
90      */
911    private LoginManager() {
92         try {
931            Configuration.setConfiguration(new HinemosClientConfig());
940        } catch (Exception e) {
95         }
961    }
97  
98     /**
99      * ログイン(接続??URLあり)
100      *
101      * @param uid
102      * @param password
103      * @throws NamingException
104      * @throws LoginException
105      *
106      * @version 2.0.0
107      * @since 2.0.0
108      */
109     public synchronized void login(String uid, String password, String url) throws Exception {
110         // ログアウ?
1115        logout();
112  
1135        m_uid = uid;
1145        m_password = password;
115  
116         // NamingContext取?
1175        m_namingcontext = getContext(url);
118  
119         try {
120             // ログインチェ?ク
1215            AccessCheckHome home = (AccessCheckHome) m_namingcontext.lookup(AccessCheckHome.JNDI_NAME);
1225            AccessCheck accessCheck = home.create();
1234            accessCheck.checkLogin();
124  
1251        } catch (Exception e) {
1261            logout();
1271            throw e;
128         }
1294    }
130  
131     /**
132      * ログアウ?
133      *
134      * @throws NamingException
135      *
136      * @version 2.0.0
137      * @since 2.0.0
138      */
139     public synchronized void logout() throws NamingException {
14010        if (m_loginContext instanceof LoginContext) {
141             try {
1425                m_loginContext.logout();
1435                m_uid = null;
144  
1450            } catch (LoginException e) {
146             }
147         }
14810        m_loginContext = null;
14910        m_namingcontext = null;
15010    }
151  
152     /**
153      * ログインチェ?ク
154      *
155      * @return
156      *
157      * @version 2.0.0
158      * @since 2.0.0
159      */
160     public boolean isLogin() {
16113        if (m_loginContext instanceof LoginContext) {
1625            return true;
163         } else {
1648            return false;
165         }
166     }
167  
168     /**
169      * RepositoryCollectorを取得します??
170      *
171      * @return RepositoryCollector
172      *
173      * @version 2.0.0
174      * @since 2.0.0
175      */
176     public synchronized NamingContext getNamingContext(String uid, String pw, String url) throws NamingException {
17713        if (!isLogin()) {
178  
179             try {
180                 // login(dialog.getUserid(), dialog.getPassword());
1818                login(uid, pw, url);
182  
1831            } catch (CommunicationException e) {
184                 // 接続失敗メ?セージを??
1851                log.error(Messages.getString("message.accesscontrol.22"), e);
1861            } catch (AccessException e) {
187                 // ログイン失敗メ?セージを??
1881                log.error(Messages.getString("message.accesscontrol.6"), e);
1892            } catch (Exception e) {
190                 // 予期せぬメ?セージを??
1912                log.error(Messages.getString("message.accesscontrol.23"), e);
1922                log.error(Messages.getString("message.accesscontrol.6"));
193             }
194  
195         }
19613        return m_namingcontext;
197     }
198  
199     /**
200      * NamingContextを取得します??
201      *
202      * @return
203      * @throws NamingException
204      * @throws LoginException
205      *
206      * @version 2.0.0
207      * @since 2.0.0
208      */
209     @SuppressWarnings("unchecked")
210     private NamingContext getContext() throws NamingException, LoginException {
211         // 接続??URL取?
2120        String url = getUrl();
213  
2140        return getContext(url);
215     }
216  
217     /**
218      * NamingContextを取得します??
219      *
220      * @return
221      * @throws NamingException
222      * @throws LoginException
223      *
224      * @version 2.0.0
225      * @since 2.0.0
226      */
227     @SuppressWarnings("unchecked")
228     private NamingContext getContext(String url) throws NamingException, LoginException {
229         // 接続??URL取?
2305        m_url = url;
231  
2325        Hashtable props = new Hashtable();
2335        props.put(InitialContext.PROVIDER_URL, m_url);
234  
2355        UsernamePasswordHandler handler = new UsernamePasswordHandler(m_uid, m_password.toCharArray());
2365        m_loginContext = new LoginContext("hinemosClient", handler);
2375        m_loginContext.login();
238  
2395        NamingContextFactory ncf = new NamingContextFactory();
2405        NamingContext namingContext = (NamingContext) ncf.getInitialContext(props);
241  
2425        return namingContext;
243     }
244  
245     /**
246      * 接続??URLを取得します??
247      *
248      * @return String 接続??URL
249      * @version 2.0.0
250      * @since 2.0.0
251      */
252     private String getUrl() {
253  
254         // 設定ファイルから接続??URLを取?
2550        String url = Config.getConfig("Login.URL");
256  
2570        return url;
258     }
259  
260     /**
261      * HinemosClient用のコンフィギュレーションクラス
262      *
263      * @version 2.0.0
264      * @since 2.0.0
265      */
266     class HinemosClientConfig extends Configuration {
267         /*
268          * (non-Javadoc)
269          *
270          * @see javax.security.auth.login.Configuration#refresh()
271          */
272         public void refresh() {
273  
274         }
275  
276         /*
277          * (non-Javadoc)
278          *
279          * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String)
280          */
281         public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
282             AppConfigurationEntry[] entry = null;
283             try {
284                 Class[] parameterTypes = {};
285                 Method m = getClass().getDeclaredMethod(name, parameterTypes);
286                 Object[] args = {};
287                 entry = (AppConfigurationEntry[]) m.invoke(this, args);
288             } catch (Exception e) {
289             }
290             return entry;
291         }
292  
293         /**
294          * Hinemos Client Configuration
295          *
296          * @return
297          */
298         @SuppressWarnings("unchecked")
299         AppConfigurationEntry[] hinemosClient() {
300             String name = "org.jboss.security.ClientLoginModule";
301             HashMap options = new HashMap();
302             options.put("multi-threaded", "true");
303             options.put("restore-login-identity", "true");
304             AppConfigurationEntry ace = new AppConfigurationEntry(name,
305                     AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
306             AppConfigurationEntry[] entry = { ace };
307             return entry;
308         }
309     }
310 }

this report was generated by version @product.version@ of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.