Line | Hits | Source |
---|---|---|
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.jboss.naming.NamingContextFactory; | |
32 | import org.jboss.security.auth.callback.UsernamePasswordHandler; | |
33 | import org.jnp.interfaces.NamingContext; | |
34 | ||
35 | import com.clustercontrol.accesscontrol.ejb.session.AccessCheck; | |
36 | import com.clustercontrol.accesscontrol.ejb.session.AccessCheckHome; | |
37 | import com.clustercontrol.util.Messages; | |
38 | ||
39 | /** | |
40 | * Hinemos Addon for RRDTool<br> | |
41 | * ログインマネージャクラス<br> | |
42 | * | |
43 | * @since 3.0.0 | |
44 | */ | |
45 | 1 | public class LoginManager { |
46 | public static final String KEY_EJB_URL = "ejbUrl"; | |
47 | public static final String VALUE_EJB_URL = "jnp://localhost:1099"; | |
48 | public static final String KEY_EJB_UID = "ejbUid"; | |
49 | public static final String VALUE_EJB_UID = "hinemos"; | |
50 | ||
51 | public static final String USER_OBJECT_ID = "com.clustercontrol.accesscontrol.user"; | |
52 | public static final String URL_OBJECT_ID = "com.clustercontrol.accesscontrol.url"; | |
53 | ||
54 | public static final String ACTION_SET_ID = "com.clustercontrol.accesscontrol.ActionSet"; | |
55 | public static final String ACTION_ID_LOGIN = "com.clustercontrol.accesscontrol.etc.action.LoginAction"; | |
56 | public static final String ACTION_ID_LOGOUT = "com.clustercontrol.accesscontrol.etc.action.LogoutAction"; | |
57 | ||
58 | 1 | private static LoginManager m_instance = null; |
59 | 1 | private NamingContext m_namingcontext = null; |
60 | 1 | private LoginContext m_loginContext = null; |
61 | 1 | private String m_url = null; |
62 | 1 | private String m_uid = null; |
63 | 1 | private String m_password = null; |
64 | ||
65 | /** | |
66 | * こ?オブジェクトを取得します?? シングルトン | |
67 | * | |
68 | * @return LoginManager ログインマネージャ | |
69 | * | |
70 | * @version 2.0.0 | |
71 | * @since 2.0.0 | |
72 | */ | |
73 | public static LoginManager getContextManager() { | |
74 | 18 | if (m_instance == null) { |
75 | 1 | m_instance = new LoginManager(); |
76 | } | |
77 | 18 | return m_instance; |
78 | } | |
79 | ||
80 | /** | |
81 | * コンストラクタ | |
82 | * | |
83 | * @version 2.0.0 | |
84 | * @since 2.0.0 | |
85 | */ | |
86 | 1 | private LoginManager() { |
87 | try { | |
88 | 1 | Configuration.setConfiguration(new HinemosClientConfig()); |
89 | 0 | } catch (Exception e) { |
90 | } | |
91 | 1 | } |
92 | ||
93 | /** | |
94 | * ログイン(接続??URLあり) | |
95 | * | |
96 | * @param uid | |
97 | * @param password | |
98 | * @throws NamingException | |
99 | * @throws LoginException | |
100 | * | |
101 | * @version 2.0.0 | |
102 | * @since 2.0.0 | |
103 | */ | |
104 | public synchronized void login(String uid, String password, String url) throws Exception { | |
105 | // ログアウ? | |
106 | 5 | logout(); |
107 | ||
108 | 5 | m_uid = uid; |
109 | 5 | m_password = password; |
110 | ||
111 | // NamingContext取? | |
112 | 5 | m_namingcontext = getContext(url); |
113 | ||
114 | try { | |
115 | // ログインチェ?ク | |
116 | 5 | AccessCheckHome home = (AccessCheckHome) m_namingcontext.lookup(AccessCheckHome.JNDI_NAME); |
117 | 5 | AccessCheck accessCheck = home.create(); |
118 | 4 | accessCheck.checkLogin(); |
119 | ||
120 | 1 | } catch (Exception e) { |
121 | 1 | logout(); |
122 | 1 | throw e; |
123 | } | |
124 | 4 | } |
125 | ||
126 | /** | |
127 | * ログアウ? | |
128 | * | |
129 | * @throws NamingException | |
130 | * | |
131 | * @version 2.0.0 | |
132 | * @since 2.0.0 | |
133 | */ | |
134 | public synchronized void logout() throws NamingException { | |
135 | 10 | if (m_loginContext instanceof LoginContext) { |
136 | try { | |
137 | 5 | m_loginContext.logout(); |
138 | 5 | m_uid = null; |
139 | ||
140 | 0 | } catch (LoginException e) { |
141 | } | |
142 | } | |
143 | 10 | m_loginContext = null; |
144 | 10 | m_namingcontext = null; |
145 | 10 | } |
146 | ||
147 | /** | |
148 | * ログインチェ?ク | |
149 | * | |
150 | * @return | |
151 | * | |
152 | * @version 2.0.0 | |
153 | * @since 2.0.0 | |
154 | */ | |
155 | public boolean isLogin() { | |
156 | 13 | if (m_loginContext instanceof LoginContext) { |
157 | 5 | return true; |
158 | } else { | |
159 | 8 | return false; |
160 | } | |
161 | } | |
162 | ||
163 | /** | |
164 | * RepositoryCollectorを取得します?? | |
165 | * | |
166 | * @return RepositoryCollector | |
167 | * | |
168 | * @version 2.0.0 | |
169 | * @since 2.0.0 | |
170 | */ | |
171 | public synchronized NamingContext getNamingContext(String uid, String pw, String url) throws NamingException { | |
172 | 13 | if (!isLogin()) { |
173 | ||
174 | try { | |
175 | // login(dialog.getUserid(), dialog.getPassword()); | |
176 | 8 | login(uid, pw, url); |
177 | ||
178 | 1 | } catch (CommunicationException e) { |
179 | // 接続失敗メ?セージを?? | |
180 | 1 | System.out.println(Messages.getString("message.accesscontrol.22")); |
181 | 1 | e.printStackTrace(); |
182 | 1 | } catch (AccessException e) { |
183 | // ログイン失敗メ?セージを?? | |
184 | 1 | System.out.println(Messages.getString("message.accesscontrol.6")); |
185 | 1 | e.printStackTrace(); |
186 | 2 | } catch (Exception e) { |
187 | // 予期せぬメ?セージを?? | |
188 | 2 | System.out.println(Messages.getString("message.accesscontrol.23")); |
189 | 2 | System.out.println(Messages.getString("message.accesscontrol.6")); |
190 | 2 | e.printStackTrace(); |
191 | } | |
192 | ||
193 | } | |
194 | 13 | return m_namingcontext; |
195 | } | |
196 | ||
197 | /** | |
198 | * NamingContextを取得します?? | |
199 | * | |
200 | * @return | |
201 | * @throws NamingException | |
202 | * @throws LoginException | |
203 | * | |
204 | * @version 2.0.0 | |
205 | * @since 2.0.0 | |
206 | */ | |
207 | @SuppressWarnings("unchecked") | |
208 | private NamingContext getContext() throws NamingException, LoginException { | |
209 | // 接続??URL取? | |
210 | 0 | String url = getUrl(); |
211 | ||
212 | 0 | return getContext(url); |
213 | } | |
214 | ||
215 | /** | |
216 | * NamingContextを取得します?? | |
217 | * | |
218 | * @return | |
219 | * @throws NamingException | |
220 | * @throws LoginException | |
221 | * | |
222 | * @version 2.0.0 | |
223 | * @since 2.0.0 | |
224 | */ | |
225 | @SuppressWarnings("unchecked") | |
226 | private NamingContext getContext(String url) throws NamingException, LoginException { | |
227 | // 接続??URL取? | |
228 | 5 | m_url = url; |
229 | ||
230 | 5 | Hashtable props = new Hashtable(); |
231 | 5 | props.put(InitialContext.PROVIDER_URL, m_url); |
232 | ||
233 | 5 | UsernamePasswordHandler handler = new UsernamePasswordHandler(m_uid, m_password.toCharArray()); |
234 | 5 | m_loginContext = new LoginContext("hinemosClient", handler); |
235 | 5 | m_loginContext.login(); |
236 | ||
237 | 5 | NamingContextFactory ncf = new NamingContextFactory(); |
238 | 5 | NamingContext namingContext = (NamingContext) ncf.getInitialContext(props); |
239 | ||
240 | 5 | return namingContext; |
241 | } | |
242 | ||
243 | /** | |
244 | * 接続??URLを取得します?? | |
245 | * | |
246 | * @return String 接続??URL | |
247 | * @version 2.0.0 | |
248 | * @since 2.0.0 | |
249 | */ | |
250 | private String getUrl() { | |
251 | ||
252 | // 設定ファイルから接続??URLを取? | |
253 | 0 | String url = Config.getConfig("Login.URL"); |
254 | ||
255 | 0 | return url; |
256 | } | |
257 | ||
258 | /** | |
259 | * HinemosClient用のコンフィギュレーションクラス | |
260 | * | |
261 | * @version 2.0.0 | |
262 | * @since 2.0.0 | |
263 | */ | |
264 | class HinemosClientConfig extends Configuration { | |
265 | /* | |
266 | * (non-Javadoc) | |
267 | * | |
268 | * @see javax.security.auth.login.Configuration#refresh() | |
269 | */ | |
270 | public void refresh() { | |
271 | ||
272 | } | |
273 | ||
274 | /* | |
275 | * (non-Javadoc) | |
276 | * | |
277 | * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String) | |
278 | */ | |
279 | public AppConfigurationEntry[] getAppConfigurationEntry(String name) { | |
280 | AppConfigurationEntry[] entry = null; | |
281 | try { | |
282 | Class[] parameterTypes = {}; | |
283 | Method m = getClass().getDeclaredMethod(name, parameterTypes); | |
284 | Object[] args = {}; | |
285 | entry = (AppConfigurationEntry[]) m.invoke(this, args); | |
286 | } catch (Exception e) { | |
287 | } | |
288 | return entry; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Hinemos Client Configuration | |
293 | * | |
294 | * @return | |
295 | */ | |
296 | @SuppressWarnings("unchecked") | |
297 | AppConfigurationEntry[] hinemosClient() { | |
298 | String name = "org.jboss.security.ClientLoginModule"; | |
299 | HashMap options = new HashMap(); | |
300 | options.put("multi-threaded", "true"); | |
301 | options.put("restore-login-identity", "true"); | |
302 | AppConfigurationEntry ace = new AppConfigurationEntry(name, | |
303 | AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); | |
304 | AppConfigurationEntry[] entry = { ace }; | |
305 | return entry; | |
306 | } | |
307 | } | |
308 | } |
this report was generated by version @product.version@ of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |