Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / service / spi / AbstractProviderManager.java @ 1314

History | View | Annotate | Download (4.07 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.ServiceException;
40

    
41
/**
42
 * Base {@link ProviderManager} implementation which stores the registered
43
 * {@link ProviderFactory} objects into a {@link Map}, using the name as the
44
 * key.
45
 * 
46
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
47
 */
48
public abstract class AbstractProviderManager implements ProviderManager_WithGetFactories {
49

    
50

    
51
        public void addProviderFactory(ProviderFactory providerFactory) {
52
                ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
53
                ExtensionPoint ep = epm.add(this.getRegistryKey(),this.getRegistryDescription());
54
            ep.append(providerFactory.getName(), null, providerFactory);
55
                providerFactory.initialize();
56
        }
57

    
58
        protected abstract String getRegistryKey();
59
        
60
        protected abstract String getRegistryDescription();
61

    
62
        public Provider createProvider(DynObject serviceParameters,
63
                        ProviderServices providerServices) throws ServiceException {
64
                String serviceName = serviceParameters.getDynClass().getName();
65
                ProviderFactory factory = getProviderFactory(serviceName);
66
                return factory == null ? null : factory.create(serviceParameters,
67
                                providerServices);
68
        }
69

    
70
        public DynObject createServiceParameters(String serviceName)
71
                        throws ServiceException {
72
                return getProviderFactory(serviceName).createParameters();
73
        }
74

    
75
    public ProviderFactory getProviderFactory(String serviceName)
76
                        throws ParametersException, NotRegisteredException {
77
                ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
78
                ExtensionPoint ep = epm.get(this.getRegistryKey());
79
                ProviderFactory factory = null; 
80

    
81
                if (ep != null) {
82
                        try {
83
                                factory = ((ProviderFactory) ep.create(serviceName));
84
                        } catch (InstantiationException e) {
85
                                throw new ParametersException(
86
                                                "Can't instance provider factory", e);
87
                        } catch (IllegalAccessException e) {
88
                                throw new ParametersException("Can't access provider factory",
89
                                                e);
90
                        }
91
                }
92

    
93
                if (factory == null) {
94
                        throw new NotRegisteredException(serviceName);
95
                }
96
                return factory;
97
        }
98
    
99
    public List getProviderFactories() {
100
            List factories = new ArrayList(); 
101
            ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
102
                ExtensionPoint ep = epm.add(this.getRegistryKey(),this.getRegistryDescription());
103

    
104
                Iterator it = ep.iterator();
105
                while( it.hasNext() ) {
106
                        ExtensionPoint.Extension extension = (ExtensionPoint.Extension) it.next();
107
                        try {
108
                                ProviderFactory factory = (ProviderFactory) extension.create();
109
                                factories.add(factory);
110
                        } catch (InstantiationException e) {
111
                                // Ignore, ProviderFactory are ExtensionSingleton and can't throw this exception
112
                        } catch (IllegalAccessException e) {
113
                                // Ignore, ProviderFactory are ExtensionSingleton and can't throw this exception
114
                        }
115
                }
116
                return factories;
117
    }
118
}