2a56c2ff0feed1e5124f49f4e4d3b06e39562eb0
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / properties / AuthenticationProviderProperty.java
1 package net.sourceforge.guacamole.net.basic.properties;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Affero General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Affero General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Affero General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 import java.lang.reflect.InvocationTargetException;
22 import net.sourceforge.guacamole.GuacamoleException;
23 import net.sourceforge.guacamole.net.auth.AuthenticationProvider;
24 import net.sourceforge.guacamole.net.basic.GuacamoleClassLoader;
25 import net.sourceforge.guacamole.properties.GuacamoleProperty;
26
27 /**
28  * A GuacamoleProperty whose value is the name of a class to use to
29  * authenticate users. This class must implement AuthenticationProvider.
30  * 
31  * @author Michael Jumper
32  */
33 public abstract class AuthenticationProviderProperty implements GuacamoleProperty<AuthenticationProvider> {
34
35     @Override
36     public AuthenticationProvider parseValue(String authProviderClassName) throws GuacamoleException {
37
38         // If no property provided, return null.
39         if (authProviderClassName == null)
40             return null;
41
42         // Get auth provider instance
43         try {
44
45             Object obj = GuacamoleClassLoader.getInstance().loadClass(authProviderClassName)
46                             .getConstructor().newInstance();
47
48             if (!(obj instanceof AuthenticationProvider))
49                 throw new GuacamoleException("Specified authentication provider class is not a AuthenticationProvider.");
50
51             return (AuthenticationProvider) obj;
52
53         }
54         catch (ClassNotFoundException e) {
55             throw new GuacamoleException("Authentication provider class not found", e);
56         }
57         catch (NoSuchMethodException e) {
58             throw new GuacamoleException("Default constructor for authentication provider not present", e);
59         }
60         catch (SecurityException e) {
61             throw new GuacamoleException("Creation of authentication provider disallowed; check your security settings", e);
62         }
63         catch (InstantiationException e) {
64             throw new GuacamoleException("Unable to instantiate authentication provider", e);
65         }
66         catch (IllegalAccessException e) {
67             throw new GuacamoleException("Unable to access default constructor of authentication provider", e);
68         }
69         catch (InvocationTargetException e) {
70             throw new GuacamoleException("Internal error in constructor of authentication provider", e.getTargetException());
71         }
72
73     }
74
75 }
76