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 javax.naming.Context;
23  import javax.naming.InitialContext;
24  import javax.sql.DataSource;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.sql.SQLException;
29  
30  /**
31   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
32   *
33   */
34  public class DataSourceConfigurableJdoFactory
35      extends AbstractConfigurableJdoFactory
36  {
37      // ----------------------------------------------------------------------
38      // Configuration
39      // ----------------------------------------------------------------------
40  
41      /**
42       * plexus.configuration
43       */
44      private String connectionFactoryName;
45  
46      /**
47       * plexus.configuration
48       */
49      private String shutdownConnectionFactoryName;
50  
51      public Properties getProperties()
52      {
53          synchronized ( configured )
54          {
55              if ( configured == Boolean.TRUE )
56              {
57                  return properties;
58              }
59              else
60              {
61                  Properties properties = new Properties();
62  
63                  Iterator it = otherProperties.entrySet().iterator();
64                  while ( it.hasNext() )
65                  {
66                      Map.Entry entry = (Map.Entry) it.next();
67                      properties.setProperty( (String) entry.getKey(), (String) entry.getValue() );
68                  }
69  
70                  setPropertyInner( properties, "javax.jdo.PersistenceManagerFactoryClass",
71                                    persistenceManagerFactoryClass );
72                  setPropertyInner( properties, "javax.jdo.option.ConnectionFactoryName", connectionFactoryName );
73  
74                  return properties;
75              }
76          }
77      }
78  
79  
80      public void shutdown()
81          throws Exception
82      {
83          if ( shutdownConnectionFactoryName != null )
84          {
85              Context ctx = new InitialContext();
86              DataSource ds = (DataSource) ctx.lookup( shutdownConnectionFactoryName );
87              try
88              {
89                  ds.getConnection();
90              }
91              catch ( SQLException e )
92              {
93                  /*
94                   * In Derby, any request to the DriverManager with a shutdown=true attribute raises an exception.
95                   * http://db.apache.org/derby/manuals/reference/sqlj251.html
96                   */
97              }
98          }
99  
100         super.shutdown();
101     }
102 
103     public String getConnectionFactoryName()
104     {
105         return connectionFactoryName;
106     }
107 
108     public void setConnectionFactoryName( String connectionFactoryName )
109     {
110         this.connectionFactoryName = connectionFactoryName;
111     }
112 
113     public String getShutdownConnectionFactoryName()
114     {
115         return shutdownConnectionFactoryName;
116     }
117 
118     public void setShutdownConnectionFactoryName( String shutdownConnectionFactoryName )
119     {
120         this.shutdownConnectionFactoryName = shutdownConnectionFactoryName;
121     }
122 }