KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > visualcontent > authentication > simplecredentials > SimpleAuthenticationService


1 /**
2  * VC Browser - Visualizes the content of a JSR 170 compatible repository
3  * Copyright (C) 2006 Sandro Böhme
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.visualcontent.authentication.simplecredentials;
19
20
21 import javax.jcr.LoginException;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24 import javax.jcr.SimpleCredentials;
25
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.visualcontent.authentication.simplecredentials.preferences.PreferenceConstants;
28 import org.visualcontent.extensionpoints.RepositoryThrowable;
29 import org.visualcontent.extensionpoints.SessionThrowable;
30 import org.visualcontent.extensionpoints.VCRepository;
31 import org.visualcontent.extensionpoints.VCSession;
32
33 public class SimpleAuthenticationService implements VCSession {
34     VCRepository repositoryConnector = null;
35     Session session = null;
36
37     public SimpleAuthenticationService() throws LoginException, RepositoryException, SessionThrowable {
38
39     }
40
41     public Session login(VCRepository repository) throws SessionThrowable, RepositoryThrowable {
42         Session session = null;
43         IPreferenceStore prefStore = SimplecredentialsPlugin.getDefault().getPreferenceStore();
44         String JavaDoc repAuthConfig = prefStore.getString(PreferenceConstants.P_REP_AUTH_CONFIG);
45         System.setProperty("java.security.auth.login.config", repAuthConfig);
46         String JavaDoc userName = prefStore.getString(PreferenceConstants.P_REP_USER_NAME);
47         String JavaDoc password = prefStore.getString(PreferenceConstants.P_REP_PASSWORD);
48         String JavaDoc workspace = prefStore.getString(PreferenceConstants.P_REP_WORKSPACE);
49         if ("".equals(workspace)){
50             workspace=null; //makes using the configured default workspace
51
}
52         try {
53             session = repository.getRepository().login(new SimpleCredentials(userName, password.toCharArray()),workspace);
54         } catch (LoginException e) {
55             SimplecredentialsPlugin.getDefault().showError("It was not possible to login. Please check the credentials.",e);
56             repository.releaseRepository();
57         } catch (RepositoryException e) {
58             SimplecredentialsPlugin.getDefault().showError("It was not possible to login. Please check the credentials.",e);
59             repository.releaseRepository();
60         } catch (RepositoryThrowable e) {
61             SimplecredentialsPlugin.getDefault().showError("It was not possible to login. Please check the credentials.",e);
62             repository.releaseRepository();
63         }
64         return session;
65     }
66
67     public void shutdownRepository() {
68         if (this.repositoryConnector!=null){
69             try {
70                 this.repositoryConnector.releaseRepository();
71             } catch (RepositoryThrowable e) {
72                 SimplecredentialsPlugin.getDefault().showError("Could not release the repository.",e);
73             }
74         }
75     }
76
77     public void logout() {
78         if (this.session!=null){
79             session.logout();
80         }
81     }
82
83 // public Session login() throws SessionThrowable {
84
// IExtensionRegistry registry = Platform.getExtensionRegistry ();
85
// IConfigurationElement[] repositories = registry.getConfigurationElementsFor("org.visualcontent.extensionpoints.VCRepository");
86
// for (int i = 0; i < repositories.length; i++) {
87
// try {
88
// Object repositoryConnectorObject = repositories[i].createExecutableExtension("class");
89
// repositoryConnector = (VCRepository) repositoryConnectorObject;
90
// String[] validSessions = repositoryConnector.getValidSessions();
91
// for (int j = 0; j < validSessions.length; j++) {
92
// if ("org.visualcontent.authentication.simplecredentials.SimpleAuthenticationService".equals(validSessions[j])){
93
// session = login(repositoryConnector);
94
// }
95
// }
96
// } catch (CoreException e) {
97
// throw new SessionThrowable("Exception while getting the repository or logging in.",e);
98
// } catch (LoginException e) {
99
// throw new SessionThrowable("Exception while getting the repository or logging in.",e);
100
// } catch (RepositoryException e) {
101
// throw new SessionThrowable("Exception while getting the repository or logging in.",e);
102
// }
103
// }
104
// return session;
105
// }
106
}
107
Popular Tags