1 package org.apache.archiva.redback.components.jdo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.annotation.PostConstruct;
26 import javax.annotation.PreDestroy;
27 import javax.jdo.JDOHelper;
28 import javax.jdo.PersistenceManagerFactory;
29 import java.sql.Connection;
30 import java.sql.DriverManager;
31 import java.sql.SQLException;
32 import java.util.Properties;
33
34
35
36
37
38 public class DefaultJdoFactory
39 implements JdoFactory
40 {
41 private Logger logger = LoggerFactory.getLogger( getClass() );
42
43 private static final String CONNECTION_DRIVER_NAME = "javax.jdo.option.ConnectionDriverName";
44
45
46
47
48 private Properties properties;
49
50 private PersistenceManagerFactory persistenceManagerFactory;
51
52 public PersistenceManagerFactory getPersistenceManagerFactory()
53 {
54 return persistenceManagerFactory;
55 }
56
57
58
59
60
61 @PostConstruct
62 public void initialize()
63 {
64 logger.info( "Initializing JDO." );
65
66 persistenceManagerFactory = JDOHelper.getPersistenceManagerFactory( properties );
67
68 String driverClass = null;
69
70 try
71 {
72 driverClass = (String) properties.get( CONNECTION_DRIVER_NAME );
73
74 if ( driverClass == null )
75 {
76 throw new RuntimeException( "Property " + CONNECTION_DRIVER_NAME + " was not set in JDO Factory." );
77 }
78
79 Thread.currentThread().getContextClassLoader().loadClass( driverClass );
80 }
81 catch ( ClassNotFoundException e )
82 {
83 throw new RuntimeException( "Cannot find driver class: " + driverClass, e );
84 }
85 }
86
87 public void shutdown()
88 throws Exception
89 {
90 dispose();
91 }
92
93 @PreDestroy
94 public void dispose()
95 {
96 if ( properties != null )
97 {
98 String databaseUrl = properties.getProperty( "javax.jdo.option.ConnectionURL" );
99
100 if ( databaseUrl != null )
101 {
102
103 if ( databaseUrl.indexOf( "jdbc:derby:" ) == 0 )
104 {
105 String databasePath = databaseUrl.substring( "jdbc:derby:".length() );
106
107 if ( databasePath.indexOf( ';' ) > 0 )
108 {
109 databasePath = databasePath.substring( 0, databasePath.indexOf( ';' ) );
110 }
111
112 Connection connection = null;
113
114 try
115 {
116
117 DriverManager.getConnection( "jdbc:derby:" + databasePath + ";shutdown=true" );
118 }
119 catch ( SQLException e )
120 {
121
122
123
124
125 }
126 finally
127 {
128 if ( connection != null )
129 {
130 try
131 {
132 connection.close();
133 }
134 catch ( SQLException e )
135 {
136
137 }
138 }
139 }
140
141 System.gc();
142 }
143 }
144 }
145 }
146 }