Source Code Cross Referenced for ConnectionManager.java in  » GIS » openjump » com » vividsolutions » jump » workbench » datastore » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » openjump » com.vividsolutions.jump.workbench.datastore 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.vividsolutions.jump.workbench.datastore;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collection;
005:        import java.util.Collections;
006:        import java.util.HashMap;
007:        import java.util.Iterator;
008:        import java.util.List;
009:        import java.util.Map;
010:
011:        import com.vividsolutions.jump.datastore.DataStoreConnection;
012:        import com.vividsolutions.jump.datastore.DataStoreDriver;
013:        import com.vividsolutions.jump.datastore.DataStoreException;
014:        import com.vividsolutions.jump.datastore.DataStoreMetadata;
015:        import com.vividsolutions.jump.datastore.Query;
016:        import com.vividsolutions.jump.io.FeatureInputStream;
017:        import com.vividsolutions.jump.util.Blackboard;
018:        import com.vividsolutions.jump.workbench.WorkbenchContext;
019:        import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
020:
021:        /**
022:         * Reuses existing connections where possible.
023:         */
024:        public class ConnectionManager {
025:
026:            public interface Listener {
027:                void connectionDescriptorAdded(
028:                        ConnectionDescriptor connectionDescriptor);
029:
030:                void connectionDescriptorRemoved(
031:                        ConnectionDescriptor connectionDescriptor);
032:            }
033:
034:            private WorkbenchContext context;
035:
036:            /**
037:             * @param connectionDescriptors
038:             *            a collection that is kept up to date by the ConnectionManager
039:             */
040:            private ConnectionManager(WorkbenchContext context,
041:                    final Collection connectionDescriptors) {
042:                this .context = context;
043:                for (Iterator i = connectionDescriptors.iterator(); i.hasNext();) {
044:                    ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
045:                            .next();
046:                    connectionDescriptorToConnectionMap.put(
047:                            connectionDescriptor, DUMMY_CONNECTION);
048:                }
049:                addListener(new Listener() {
050:                    public void connectionDescriptorAdded(
051:                            ConnectionDescriptor connectionDescriptor) {
052:                        updateConnectionDescriptors();
053:                    }
054:
055:                    public void connectionDescriptorRemoved(
056:                            ConnectionDescriptor connectionDescriptor) {
057:                        updateConnectionDescriptors();
058:                    }
059:
060:                    private void updateConnectionDescriptors() {
061:                        connectionDescriptors.clear();
062:                        connectionDescriptors
063:                                .addAll(connectionDescriptorToConnectionMap
064:                                        .keySet());
065:                    }
066:                });
067:            }
068:
069:            private Map connectionDescriptorToConnectionMap = new HashMap();
070:
071:            public DataStoreConnection getOpenConnection(
072:                    ConnectionDescriptor connectionDescriptor) throws Exception {
073:                if (getConnection(connectionDescriptor).isClosed()) {
074:                    connectionDescriptorToConnectionMap
075:                            .put(
076:                                    connectionDescriptor,
077:                                    connectionDescriptor
078:                                            .createConnection(getDriver(connectionDescriptor
079:                                                    .getDataStoreDriverClassName())));
080:                }
081:                return getConnection(connectionDescriptor);
082:            }
083:
084:            public DataStoreDriver getDriver(String driverClassName) {
085:                DataStoreDriver driver = findDriverRegistryEntry(driverClassName);
086:                if (driver == null)
087:                    throw new RuntimeException("Can't find DataStoreDriver: "
088:                            + driverClassName);
089:                return driver;
090:            }
091:
092:            private DataStoreDriver findDriverRegistryEntry(
093:                    String driverClassName) {
094:                List drivers = context.getRegistry().getEntries(
095:                        DataStoreDriver.REGISTRY_CLASSIFICATION);
096:                for (Iterator i = drivers.iterator(); i.hasNext();) {
097:                    DataStoreDriver driver = (DataStoreDriver) i.next();
098:                    if (driver.getClass().getName().equals(driverClassName))
099:                        return driver;
100:                }
101:                return null;
102:            }
103:
104:            private static final DataStoreConnection DUMMY_CONNECTION = new DataStoreConnection() {
105:                public DataStoreMetadata getMetadata() {
106:                    throw new UnsupportedOperationException();
107:                }
108:
109:                public FeatureInputStream execute(Query query) {
110:                    throw new UnsupportedOperationException();
111:                }
112:
113:                public void close() throws DataStoreException {
114:                    throw new UnsupportedOperationException();
115:                }
116:
117:                public boolean isClosed() throws DataStoreException {
118:                    return true;
119:                }
120:            };
121:
122:            /**
123:             * @return a connection, possibly closed, never null
124:             */
125:            public DataStoreConnection getConnection(
126:                    ConnectionDescriptor connectionDescriptor) {
127:                if (!connectionDescriptorToConnectionMap
128:                        .containsKey(connectionDescriptor)) {
129:                    connectionDescriptorToConnectionMap.put(
130:                            connectionDescriptor, DUMMY_CONNECTION);
131:                    fireConnectionDescriptorAdded(connectionDescriptor);
132:                }
133:                return (DataStoreConnection) connectionDescriptorToConnectionMap
134:                        .get(connectionDescriptor);
135:            }
136:
137:            public Collection getConnectionDescriptors() {
138:                return Collections
139:                        .unmodifiableCollection(connectionDescriptorToConnectionMap
140:                                .keySet());
141:            }
142:
143:            /**
144:             * Removes the ConnectionDescriptor and closes its associated
145:             * DataStoreConnection.
146:             */
147:            public void deleteConnectionDescriptor(
148:                    ConnectionDescriptor connectionDescriptor)
149:                    throws DataStoreException {
150:                if (!getConnection(connectionDescriptor).isClosed()) {
151:                    getConnection(connectionDescriptor).close();
152:                }
153:                connectionDescriptorToConnectionMap
154:                        .remove(connectionDescriptor);
155:                fireConnectionDescriptorRemoved(connectionDescriptor);
156:            }
157:
158:            private void fireConnectionDescriptorAdded(
159:                    ConnectionDescriptor connectionDescriptor) {
160:                for (Iterator i = listeners.iterator(); i.hasNext();) {
161:                    Listener listener = (Listener) i.next();
162:                    listener.connectionDescriptorAdded(connectionDescriptor);
163:                }
164:            }
165:
166:            private void fireConnectionDescriptorRemoved(
167:                    ConnectionDescriptor connectionDescriptor) {
168:                for (Iterator i = listeners.iterator(); i.hasNext();) {
169:                    Listener listener = (Listener) i.next();
170:                    listener.connectionDescriptorRemoved(connectionDescriptor);
171:                }
172:            }
173:
174:            public static ConnectionManager instance(WorkbenchContext context) {
175:                Blackboard blackboard = context.getBlackboard();
176:                String INSTANCE_KEY = ConnectionManager.class.getName()
177:                        + " - INSTANCE";
178:                if (blackboard.get(INSTANCE_KEY) == null) {
179:                    // If the blackboard has an associated PersistentBlackboard,
180:                    // that will be used to persist the DataStoreDrivers.
181:                    // [Jon Aquino 2005-03-11]
182:                    blackboard.put(INSTANCE_KEY, new ConnectionManager(context,
183:                            (Collection) PersistentBlackboardPlugIn.get(
184:                                    blackboard).get(
185:                                    ConnectionManager.class.getName()
186:                                            + " - CONNECTION DESCRIPTORS",
187:                                    new ArrayList())));
188:                }
189:                return (ConnectionManager) blackboard.get(INSTANCE_KEY);
190:            }
191:
192:            private List listeners = new ArrayList();
193:
194:            public void addListener(Listener listener) {
195:                listeners.add(listener);
196:            }
197:
198:            public void closeConnections() throws DataStoreException {
199:                for (Iterator i = getConnectionDescriptors().iterator(); i
200:                        .hasNext();) {
201:                    ConnectionDescriptor connectionDescriptor = (ConnectionDescriptor) i
202:                            .next();
203:                    if (!getConnection(connectionDescriptor).isClosed()) {
204:                        getConnection(connectionDescriptor).close();
205:                    }
206:                }
207:            }
208:
209:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.