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.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 | */ | |
47 | public 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 | ||
60 | private static LoginManager m_instance = null; | |
61 | private NamingContext m_namingcontext = null; | |
62 | private LoginContext m_loginContext = null; | |
63 | private String m_url = null; | |
64 | private String m_uid = null; | |
65 | private String m_password = null; | |
66 | ||
67 | // 実行ログ出力用クラス | |
68 | 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() { | |
79 | if (m_instance == null) { | |
80 | m_instance = new LoginManager(); | |
81 | } | |
82 | return m_instance; | |
83 | } | |
84 | ||
85 | /** | |
86 | * コンストラクタ | |
87 | * | |
88 | * @version 2.0.0 | |
89 | * @since 2.0.0 | |
90 | */ | |
91 | private LoginManager() { | |
92 | try { | |
93 | Configuration.setConfiguration(new HinemosClientConfig()); | |
94 | } catch (Exception e) { | |
95 | } | |
96 | } | |
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 | // ログアウ? | |
111 | logout(); | |
112 | ||
113 | m_uid = uid; | |
114 | m_password = password; | |
115 | ||
116 | // NamingContext取? | |
117 | m_namingcontext = getContext(url); | |
118 | ||
119 | try { | |
120 | // ログインチェ?ク | |
121 | AccessCheckHome home = (AccessCheckHome) m_namingcontext.lookup(AccessCheckHome.JNDI_NAME); | |
122 | AccessCheck accessCheck = home.create(); | |
123 | accessCheck.checkLogin(); | |
124 | ||
125 | } catch (Exception e) { | |
126 | logout(); | |
127 | throw e; | |
128 | } | |
129 | } | |
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 { | |
140 | if (m_loginContext instanceof LoginContext) { | |
141 | try { | |
142 | m_loginContext.logout(); | |
143 | m_uid = null; | |
144 | ||
145 | } catch (LoginException e) { | |
146 | } | |
147 | } | |
148 | m_loginContext = null; | |
149 | m_namingcontext = null; | |
150 | } | |
151 | ||
152 | /** | |
153 | * ログインチェ?ク | |
154 | * | |
155 | * @return | |
156 | * | |
157 | * @version 2.0.0 | |
158 | * @since 2.0.0 | |
159 | */ | |
160 | public boolean isLogin() { | |
161 | if (m_loginContext instanceof LoginContext) { | |
162 | return true; | |
163 | } else { | |
164 | 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 { | |
177 | if (!isLogin()) { | |
178 | ||
179 | try { | |
180 | // login(dialog.getUserid(), dialog.getPassword()); | |
181 | login(uid, pw, url); | |
182 | ||
183 | } catch (CommunicationException e) { | |
184 | // 接続失敗メ?セージを?? | |
185 | log.error(Messages.getString("message.accesscontrol.22"), e); | |
186 | } catch (AccessException e) { | |
187 | // ログイン失敗メ?セージを?? | |
188 | log.error(Messages.getString("message.accesscontrol.6"), e); | |
189 | } catch (Exception e) { | |
190 | // 予期せぬメ?セージを?? | |
191 | log.error(Messages.getString("message.accesscontrol.23"), e); | |
192 | log.error(Messages.getString("message.accesscontrol.6")); | |
193 | } | |
194 | ||
195 | } | |
196 | 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取? | |
212 | String url = getUrl(); | |
213 | ||
214 | 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取? | |
230 | m_url = url; | |
231 | ||
232 | Hashtable props = new Hashtable(); | |
233 | props.put(InitialContext.PROVIDER_URL, m_url); | |
234 | ||
235 | UsernamePasswordHandler handler = new UsernamePasswordHandler(m_uid, m_password.toCharArray()); | |
236 | m_loginContext = new LoginContext("hinemosClient", handler); | |
237 | m_loginContext.login(); | |
238 | ||
239 | NamingContextFactory ncf = new NamingContextFactory(); | |
240 | NamingContext namingContext = (NamingContext) ncf.getInitialContext(props); | |
241 | ||
242 | 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を取? | |
255 | String url = Config.getConfig("Login.URL"); | |
256 | ||
257 | return url; | |
258 | } | |
259 | ||
260 | /** | |
261 | * HinemosClient用のコンフィギュレーションクラス | |
262 | * | |
263 | * @version 2.0.0 | |
264 | * @since 2.0.0 | |
265 | */ | |
266 | 1 | class HinemosClientConfig extends Configuration { |
267 | /* | |
268 | * (non-Javadoc) | |
269 | * | |
270 | * @see javax.security.auth.login.Configuration#refresh() | |
271 | */ | |
272 | public void refresh() { | |
273 | ||
274 | 0 | } |
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 | 5 | AppConfigurationEntry[] entry = null; |
283 | try { | |
284 | 5 | Class[] parameterTypes = {}; |
285 | 5 | Method m = getClass().getDeclaredMethod(name, parameterTypes); |
286 | 5 | Object[] args = {}; |
287 | 5 | entry = (AppConfigurationEntry[]) m.invoke(this, args); |
288 | 0 | } catch (Exception e) { |
289 | } | |
290 | 5 | return entry; |
291 | } | |
292 | ||
293 | /** | |
294 | * Hinemos Client Configuration | |
295 | * | |
296 | * @return | |
297 | */ | |
298 | @SuppressWarnings("unchecked") | |
299 | AppConfigurationEntry[] hinemosClient() { | |
300 | 5 | String name = "org.jboss.security.ClientLoginModule"; |
301 | 5 | HashMap options = new HashMap(); |
302 | 5 | options.put("multi-threaded", "true"); |
303 | 5 | options.put("restore-login-identity", "true"); |
304 | 10 | AppConfigurationEntry ace = new AppConfigurationEntry(name, |
305 | 5 | AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); |
306 | 5 | AppConfigurationEntry[] entry = { ace }; |
307 | 5 | return entry; |
308 | } | |
309 | } | |
310 | } |
this report was generated by version @product.version@ of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |