1 package com.atlassian.user.impl.osuser.config.xml;
2
3
4
5
6
7 import com.atlassian.cache.CacheFactory;
8 import com.atlassian.user.cache.CacheFactoryAware;
9 import com.atlassian.user.configuration.ConfigurationException;
10 import com.atlassian.user.impl.osuser.DefaultOSUAccessor;
11 import com.atlassian.user.impl.osuser.OSUAccessor;
12 import com.opensymphony.user.provider.AccessProvider;
13 import com.opensymphony.user.provider.CredentialsProvider;
14 import com.opensymphony.user.provider.ProfileProvider;
15 import com.opensymphony.user.provider.UserProvider;
16 import com.opensymphony.util.ClassLoaderUtil;
17 import org.apache.log4j.Logger;
18 import org.xml.sax.SAXException;
19 import org.xml.sax.helpers.DefaultHandler;
20
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.parsers.SAXParser;
23 import javax.xml.parsers.SAXParserFactory;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Properties;
29
30 public class DefaultOSUConfigurationLoader implements OSUConfigurationLoader
31 {
32 private static final Logger log = Logger.getLogger(DefaultOSUConfigurationLoader.class);
33 private DefaultOSUConfigurationHandler configHandler;
34 private OSUAccessor accessor = new DefaultOSUAccessor();
35 private ProfileProvider profileProvider;
36 private AccessProvider accessProvider;
37 private CredentialsProvider credentialsProvider;
38 private List<CredentialsProvider> credentialProviders = new ArrayList<CredentialsProvider>();
39 private CacheFactory cacheFactory;
40
41 public synchronized void load(InputStream in) throws ConfigurationException
42 {
43 log.debug("Loading config");
44 try
45 {
46 SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
47 parser.parse(in, getOSUserConfigurationHandler());
48 }
49 catch (SAXException e)
50 {
51 log.error("Could not parse config XML", e);
52 throw new ConfigurationException(e.getMessage());
53 }
54 catch (IOException e)
55 {
56 log.error("Could not read config from stream", e);
57 throw new ConfigurationException(e.getMessage());
58 }
59 catch (ParserConfigurationException e)
60 {
61 log.fatal("Could not obtain SAX parser", e);
62 throw new ConfigurationException(e.getMessage());
63 }
64 catch (RuntimeException e)
65 {
66 log.fatal("RuntimeException", e);
67 throw e;
68 }
69 catch (Throwable e)
70 {
71 log.fatal("Exception", e);
72 throw new ConfigurationException(e.getMessage());
73 }
74 }
75
76 public DefaultHandler getOSUserConfigurationHandler()
77 {
78 if (configHandler == null)
79 configHandler = new DefaultOSUConfigurationHandler(this);
80
81 return configHandler;
82 }
83
84 public void setOSUserConfigurationHandler(DefaultOSUConfigurationHandler configHandler)
85 {
86 this.configHandler = configHandler;
87 }
88
89 public void addProvider(String providerClassName, Properties providerProperties) throws ConfigurationException
90 {
91 if (log.isDebugEnabled())
92 {
93 log.debug("UserProvider class = " + providerClassName + " " + providerProperties);
94 }
95
96 try
97 {
98 UserProvider provider;
99 provider = (UserProvider) ClassLoaderUtil.loadClass(providerClassName, this.getClass()).newInstance();
100
101 if (provider == null)
102 {
103 throw new ConfigurationException("OSUser provider class was unable to be instantiated: [" + providerClassName + "]");
104 }
105
106
107 if (provider instanceof CacheFactoryAware)
108 ((CacheFactoryAware) provider).setCacheFactory(cacheFactory);
109
110 if (provider.init(providerProperties))
111 {
112 if (provider instanceof AccessProvider)
113 {
114 if (accessor != null)
115 accessor.setAccessProvider((AccessProvider) provider);
116
117 accessProvider = (AccessProvider) provider;
118 }
119
120 if (provider instanceof CredentialsProvider)
121 {
122 CredentialsProvider credentialsProvider = (CredentialsProvider) provider;
123 if (accessor != null)
124 accessor.setCredentialsProvider(credentialsProvider);
125
126
127 if (!credentialProviders.contains(credentialsProvider))
128 credentialProviders.add(credentialsProvider);
129
130 this.credentialsProvider = (CredentialsProvider) provider;
131 }
132
133 if (provider instanceof ProfileProvider)
134 {
135 if (accessor != null)
136 accessor.setProfileProvider((ProfileProvider) provider);
137 profileProvider = (ProfileProvider) provider;
138 }
139 }
140 else
141 {
142 log.error("Could not getConnectionPoolProperties provider " + providerClassName);
143 throw new ConfigurationException("Could not getConnectionPoolProperties provider " + providerClassName);
144 }
145 }
146 catch (Exception e)
147 {
148 log.error("Could not create instance of provider [" + providerClassName + "]: ", e);
149 throw new ConfigurationException(e.getMessage());
150 }
151 }
152
153 public void setAccessor(DefaultOSUAccessor accessor)
154 {
155 this.accessor = accessor;
156 }
157
158 public OSUAccessor getOSUAccessor()
159 {
160 return accessor;
161 }
162
163 public List getCredentialProviders()
164 {
165 return credentialProviders;
166 }
167
168 public ProfileProvider getProfileProvider()
169 {
170 return profileProvider;
171 }
172
173 public AccessProvider getAccessProvider()
174 {
175 return accessProvider;
176 }
177
178 public CredentialsProvider getCredentialsProvider()
179 {
180 return credentialsProvider;
181 }
182
183 public void setCacheFactory(CacheFactory cacheFactory)
184 {
185 this.cacheFactory = cacheFactory;
186 }
187 }