d1f6a87ab206099fc86d6180e9eeb20600fdee9d
[utils] / security / src / main / java / org / wamblee / usermgt / UserAdministrationComponent.java
1 /*
2  * Copyright 2008 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.usermgt;
17
18 import java.io.IOException;
19
20 import javax.sql.DataSource;
21
22 import org.springframework.orm.hibernate3.HibernateTemplate;
23 import org.springframework.transaction.PlatformTransactionManager;
24 import org.wamblee.cache.EhCache;
25 import org.wamblee.system.adapters.DefaultContainer;
26 import org.wamblee.system.core.Component;
27 import org.wamblee.system.core.DefaultProvidedInterface;
28 import org.wamblee.system.core.DefaultRequiredInterface;
29 import org.wamblee.system.core.ProvidedInterface;
30 import org.wamblee.system.core.Scope;
31 import org.wamblee.usermgt.hibernate.UsermgtHibernateMappingFiles;
32
33 public class UserAdministrationComponent extends DefaultContainer {
34
35     private ProvidedInterface TRANSACTION_MGR = new DefaultProvidedInterface(
36             "transactionManager", PlatformTransactionManager.class);
37     private ProvidedInterface USER_CACHE = new DefaultProvidedInterface(
38             "userCache", EhCache.class);
39     private ProvidedInterface HIBERNATE_TEMPLATE = new DefaultProvidedInterface(
40             "hibernateTemplate", HibernateTemplate.class);
41     private ProvidedInterface USER_MGT = new DefaultProvidedInterface(
42             "usermgt", UserAdministration.class);
43
44     private Component<?> _hibernate;
45     private Component<?> _repository;
46     private Component<?> _usermgt;
47
48     private boolean _exposeInternals;
49
50     public UserAdministrationComponent(String aName, boolean aExposeInternals)
51             throws IOException {
52         super(aName);
53         _exposeInternals = aExposeInternals;
54
55         addComponent("mappingFiles", new UsermgtHibernateMappingFiles());
56
57         _hibernate = new HibernateComponent("hibernate");
58         addComponent(_hibernate);
59
60         _repository = new UserGroupRepositoryComponent("usersgroups");
61         addComponent(_repository);
62
63         _usermgt = new UserAdministrationLightComponent("usermgtlight");
64         addComponent(_usermgt);
65
66         addRequiredInterface(new DefaultRequiredInterface("datasource",
67                 DataSource.class));
68
69         if (aExposeInternals) {
70             addProvidedInterface(TRANSACTION_MGR);
71             addProvidedInterface(USER_CACHE);
72             addProvidedInterface(HIBERNATE_TEMPLATE);
73         }
74         addProvidedInterface(USER_MGT);
75     }
76
77     @Override
78     protected Scope doStart(Scope aExternalScope) {
79
80         Scope scope = super.doStart(aExternalScope);
81         if (_exposeInternals) {
82             addInterface(TRANSACTION_MGR, getInterfaceImplementation(
83                     TRANSACTION_MGR, _hibernate, scope), aExternalScope);
84             addInterface(USER_CACHE, getInterfaceImplementation(USER_CACHE,
85                     _repository, scope), aExternalScope);
86             addInterface(HIBERNATE_TEMPLATE, getInterfaceImplementation(
87                     HIBERNATE_TEMPLATE, _hibernate, scope), aExternalScope);
88         }
89         addInterface(USER_MGT, getInterfaceImplementation(USER_MGT, _usermgt,
90                 scope), aExternalScope);
91
92         return scope;
93     }
94
95     private Object getInterfaceImplementation(ProvidedInterface aInterface,
96             Component aSource, Scope aScope) {
97         for (ProvidedInterface provided : aSource.getProvidedInterfaces()) {
98             if (provided.equals(aInterface)) {
99                 return aScope
100                         .getInterfaceImplementation(provided, Object.class);
101             }
102         }
103         throw new IllegalArgumentException("Provided interface '" + aInterface
104                 + "' not found in component '" + aSource + "'");
105     }
106 }