gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / service / spi / AbstractProviderManager.java @ 670
History | View | Annotate | Download (3.18 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {} {{Task}}
|
26 |
*/
|
27 |
package org.gvsig.tools.service.spi; |
28 |
|
29 |
import java.util.Map; |
30 |
|
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dynobject.DynObject; |
33 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
35 |
import org.gvsig.tools.service.ServiceException; |
36 |
|
37 |
/**
|
38 |
* Base {@link ProviderManager} implementation which stores the registered
|
39 |
* {@link ProviderFactory} objects into a {@link Map}, using the name as the
|
40 |
* key.
|
41 |
*
|
42 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
43 |
*/
|
44 |
public abstract class AbstractProviderManager implements ProviderManager { |
45 |
|
46 |
|
47 |
public void addProviderFactory(ProviderFactory providerFactory) { |
48 |
ExtensionPointManager epm = ToolsLocator.getExtensionPointManager(); |
49 |
ExtensionPoint ep = epm.add(this.getRegistryKey(),this.getRegistryDescription()); |
50 |
ep.append(providerFactory.getName(), null, providerFactory);
|
51 |
providerFactory.initialize(); |
52 |
} |
53 |
|
54 |
protected abstract String getRegistryKey(); |
55 |
|
56 |
protected abstract String getRegistryDescription(); |
57 |
|
58 |
public Provider createProvider(DynObject serviceParameters, |
59 |
ProviderServices providerServices) throws ServiceException {
|
60 |
String serviceName = serviceParameters.getDynClass().getName();
|
61 |
ProviderFactory factory = getProviderFactory(serviceName); |
62 |
return factory == null ? null : factory.create(serviceParameters, |
63 |
providerServices); |
64 |
} |
65 |
|
66 |
public DynObject createServiceParameters(String serviceName) |
67 |
throws ServiceException {
|
68 |
return getProviderFactory(serviceName).createParameters();
|
69 |
} |
70 |
|
71 |
protected ProviderFactory getProviderFactory(String serviceName) |
72 |
throws ParametersException, NotRegisteredException {
|
73 |
ExtensionPointManager epm = ToolsLocator.getExtensionPointManager(); |
74 |
ExtensionPoint ep = epm.get(this.getRegistryKey());
|
75 |
ProviderFactory factory = null;
|
76 |
|
77 |
if (ep != null) { |
78 |
try {
|
79 |
factory = ((ProviderFactory) ep.create(serviceName)); |
80 |
} catch (InstantiationException e) { |
81 |
throw new ParametersException( |
82 |
"Can't instance provider factory", e);
|
83 |
} catch (IllegalAccessException e) { |
84 |
throw new ParametersException("Can't access provider factory", |
85 |
e); |
86 |
} |
87 |
} |
88 |
|
89 |
if (factory == null) { |
90 |
throw new NotRegisteredException(serviceName); |
91 |
} |
92 |
return factory;
|
93 |
} |
94 |
} |