View Javadoc

1   package org.apache.archiva.redback.components.jdo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author David Wynter
36   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
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       * @configuration
47       */
48      private Properties properties;
49  
50      private PersistenceManagerFactory persistenceManagerFactory;
51  
52      public PersistenceManagerFactory getPersistenceManagerFactory()
53      {
54          return persistenceManagerFactory;
55      }
56  
57      // ----------------------------------------------------------------------
58      // Component Lifecycle
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                         /* shutdown the database */
117                         DriverManager.getConnection( "jdbc:derby:" + databasePath + ";shutdown=true" );
118                     }
119                     catch ( SQLException e )
120                     {
121                         /*
122                          * In Derby, any request to the DriverManager with a shutdown=true attribute raises an exception.
123                          * http://db.apache.org/derby/manuals/reference/sqlj251.html
124                          */
125                     }
126                     finally
127                     {
128                         if ( connection != null )
129                         {
130                             try
131                             {
132                                 connection.close();
133                             }
134                             catch ( SQLException e )
135                             {
136                                 // ignore
137                             }
138                         }
139                     }
140 
141                     System.gc();
142                 }
143             }
144         }
145     }
146 }