KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > client > prefs > LoginInfoStoreFactory


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.appserv.management.client.prefs;
24
25 import java.util.Arrays JavaDoc;
26
27 /** A factory class to create instances of LoginInfoStore.
28  * @since Appserver 9.0
29  */

30 public class LoginInfoStoreFactory {
31     
32     /** Private constructor.
33      */

34     private LoginInfoStoreFactory() {
35     }
36     
37     /** Returns the store that is represented by given class name. The parameter must
38      * implement the {@link LoginInfoStore} interface. If a null is passed, an instance of the default
39      * store {@link MemoryHashLoginInfoStore} is returned.
40      * @param storeImplClassName fully qualified name of the class implementing LoginInfoStore. May be null.
41      * @return the instance of LoginInfoStore of your choice
42      * @throws IllegalArgumentException if the parameter does not implement LoginInfoStore
43      * @throws StoreException if the construction of default store results in problems
44      * @throws ClassNotFoundException if the given class could not be loaded
45      */

46     public static LoginInfoStore getStore(final String JavaDoc storeImplClassName)
47         throws StoreException, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
48         LoginInfoStore store = null;
49         if (storeImplClassName == null)
50             store = getDefaultStore();
51         else
52             store = getCustomStore(storeImplClassName);
53         return ( store );
54     }
55     
56     private static LoginInfoStore getDefaultStore() throws StoreException {
57         return ( new MemoryHashLoginInfoStore() );
58     }
59     
60     private static LoginInfoStore getCustomStore(final String JavaDoc icn)
61         throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc{
62         final Class JavaDoc ic = Class.forName(icn);
63         final String JavaDoc in = LoginInfoStore.class.getName();
64         if (ic == null || !isStore(ic))
65             throw new IllegalArgumentException JavaDoc("Class: " + ic.getName() + " does not implement: " + in);
66         final LoginInfoStore store = (LoginInfoStore) ic.newInstance();
67         return ( store );
68     }
69     
70     private static boolean isStore(final Class JavaDoc c) {
71         final Class JavaDoc[] ifs = c.getInterfaces();
72         final Class JavaDoc sc = LoginInfoStore.class;
73         return ( Arrays.asList(ifs).contains(sc) );
74     }
75 }
Popular Tags