Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / service / spi / AbstractServiceManager.java @ 944

History | View | Annotate | Download (4.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2009 {}  {{Task}}
27
 */
28
package org.gvsig.tools.service.spi;
29

    
30
import java.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Map;
34

    
35
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.extensionpoint.ExtensionPoint;
38
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
39
import org.gvsig.tools.service.Service;
40
import org.gvsig.tools.service.ServiceException;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * Base {@link ServiceManager} implementation which stores the registered
46
 * {@link ServiceFactory} objects into a {@link Map}, using the name as the key.
47
 * 
48
 * @author 2009- C?sar Ordi?ana - gvSIG team
49
 */
50
public abstract class AbstractServiceManager implements ServiceManager {
51
        private static final Logger logger = LoggerFactory.getLogger(AbstractServiceManager.class);
52

    
53
        public void addServiceFactory(ServiceFactory serviceFactory) {
54
                ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
55
                ExtensionPoint ep = epm.add(this.getRegistryKey(), this
56
                                .getRegistryDescription());
57
                ep.append(serviceFactory.getName(), null, serviceFactory);
58
                serviceFactory.initialize();
59
                logger.info(this.getName()+" register service '"+serviceFactory.getName()+"' factory "+serviceFactory.getClass().getName()+".");
60
        }
61

    
62
        protected String getName() {
63
                String s = this.getClass().getName(); // .getSimpleName() snif
64
                try {
65
                        String[] x = s.split(".");
66
                        return x[x.length-1];
67
                } catch(Exception ex) {
68
                        return s;
69
                }
70
        }
71
        
72
        protected abstract String getRegistryKey();
73

    
74
        protected abstract String getRegistryDescription();
75

    
76
        public Service createService(DynObject serviceParameters)
77
                        throws ServiceException {
78
                String serviceName = serviceParameters.getDynClass().getName();
79
                ServiceFactory factory = getServiceFactory(serviceName);
80
                return factory == null ? null : factory.create(serviceParameters, this);
81
        }
82

    
83
        public DynObject createServiceParameters(String serviceName)
84
                        throws ServiceException {
85
                return getServiceFactory(serviceName).createParameters();
86
        }
87

    
88
        private ServiceFactory getServiceFactory(String serviceName)
89
                        throws ParametersException, NotRegisteredException {
90
                ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
91
                ExtensionPoint ep = epm.get(this.getRegistryKey());
92
                ServiceFactory factory = null;
93
                try {
94
                        factory = ((ServiceFactory) ep.create(serviceName));
95
                } catch (InstantiationException e) {
96
                        throw new ParametersException("Can't instance service factory", e);
97
                } catch (IllegalAccessException e) {
98
                        throw new ParametersException("Can't access service factory", e);
99
                }
100

    
101
                if (factory == null) {
102
                        throw new NotRegisteredException(serviceName);
103
                }
104
                return factory;
105
        }
106
        
107
        public List getServiceFactories() {
108
            List factories = new ArrayList(); 
109
            ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
110
                ExtensionPoint ep = epm.add(this.getRegistryKey(),this.getRegistryDescription());
111

    
112
                Iterator it = ep.iterator();
113
                while( it.hasNext() ) {
114
                        ExtensionPoint.Extension extension = (ExtensionPoint.Extension) it.next();
115
                        try {
116
                                ServiceFactory factory = (ServiceFactory) extension.create();
117
                                factories.add(factory);
118
                        } catch (InstantiationException e) {
119
                                // Ignore, ServiceFactory are ExtensionSingleton and can't throw this exception
120
                        } catch (IllegalAccessException e) {
121
                                // Ignore, ServiceFactory are ExtensionSingleton and can't throw this exception
122
                        }
123
                }
124
                return factories;
125
    }
126
}