svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / RegisterImpl.java @ 43020
History | View | Annotate | Download (3.62 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.impl; |
---|---|---|---|
2 | |||
3 | import java.util.Iterator; |
||
4 | import java.util.List; |
||
5 | import org.gvsig.fmap.dal.DataParameters; |
||
6 | import org.gvsig.fmap.dal.DataFactory; |
||
7 | import org.gvsig.fmap.dal.DataFactoryUnit; |
||
8 | import org.gvsig.fmap.dal.Register; |
||
9 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
10 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
11 | import org.gvsig.tools.ToolsLocator; |
||
12 | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
||
13 | import org.gvsig.tools.service.spi.Services; |
||
14 | |||
15 | public class RegisterImpl implements Register { |
||
16 | |||
17 | private class FactoriesIterator implements Iterator<DataFactory> { |
||
18 | |||
19 | private final Iterator<ExtensionPoint.Extension> factories; |
||
20 | |||
21 | public FactoriesIterator(Iterator<ExtensionPoint.Extension> factories) { |
||
22 | this.factories = factories;
|
||
23 | } |
||
24 | |||
25 | @Override
|
||
26 | public boolean hasNext() { |
||
27 | return this.factories.hasNext(); |
||
28 | } |
||
29 | |||
30 | @Override
|
||
31 | public DataFactory next() {
|
||
32 | try {
|
||
33 | ExtensionPoint.Extension x = this.factories.next();
|
||
34 | return (DataFactory) x.create();
|
||
35 | } catch (Exception ex) { |
||
36 | throw new RuntimeException("Can't retrive next factory.", ex); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | @Override
|
||
41 | public void remove() { |
||
42 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
43 | } |
||
44 | |||
45 | } |
||
46 | |||
47 | private final String name; |
||
48 | private final String description; |
||
49 | |||
50 | public RegisterImpl(
|
||
51 | String name,
|
||
52 | String description
|
||
53 | ) { |
||
54 | this.name = name;
|
||
55 | this.description = description;
|
||
56 | ToolsLocator.getExtensionPointManager().add( |
||
57 | this.name,
|
||
58 | this.description
|
||
59 | ); |
||
60 | } |
||
61 | |||
62 | private ExtensionPoint getFactories() {
|
||
63 | ExtensionPoint factories = ToolsLocator.getExtensionPointManager().get( |
||
64 | this.name
|
||
65 | ); |
||
66 | return factories;
|
||
67 | } |
||
68 | |||
69 | @Override
|
||
70 | public void register(DataFactory factory) { |
||
71 | ExtensionPoint factories = this.getFactories();
|
||
72 | factories.append( |
||
73 | factory.getName(), |
||
74 | factory.getDescription(), |
||
75 | factory |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | @Override
|
||
80 | public DataFactory getFactory(String name) { |
||
81 | try {
|
||
82 | ExtensionPoint factories = this.getFactories();
|
||
83 | DataFactory factory = (DataFactory) factories.create(name); |
||
84 | return factory;
|
||
85 | } catch (Exception ex) { |
||
86 | return null; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | @Override
|
||
91 | public DataFactoryUnit create(
|
||
92 | String name,
|
||
93 | DataParameters parameters, |
||
94 | Services services |
||
95 | ) throws ProviderNotRegisteredException, InitializeException {
|
||
96 | DataFactory factory = this.getFactory(name);
|
||
97 | if( factory == null ) { |
||
98 | throw new ProviderNotRegisteredException(name); |
||
99 | } |
||
100 | DataFactoryUnit unit = factory.create( |
||
101 | parameters, services |
||
102 | ); |
||
103 | return unit;
|
||
104 | } |
||
105 | |||
106 | @Override
|
||
107 | public boolean exits(String name) { |
||
108 | ExtensionPoint factories = this.getFactories();
|
||
109 | return factories.has(name);
|
||
110 | } |
||
111 | |||
112 | @Override
|
||
113 | public List<String> getFactoryNames() { |
||
114 | ExtensionPoint factories = this.getFactories();
|
||
115 | List<String> names = factories.getNames(); |
||
116 | return names;
|
||
117 | } |
||
118 | |||
119 | @Override
|
||
120 | public Iterator<DataFactory> iterator() { |
||
121 | ExtensionPoint factories = this.getFactories();
|
||
122 | return new FactoriesIterator(factories.iterator()); |
||
123 | } |
||
124 | |||
125 | |||
126 | } |