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 java.sql.Connection;
23  import java.sql.DriverManager;
24  import java.sql.SQLException;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  /**
30   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
31   */
32  public class DefaultConfigurableJdoFactory
33      extends AbstractConfigurableJdoFactory
34      implements ConfigurableJdoFactory
35  {
36      // ----------------------------------------------------------------------
37      // Configuration
38      // ----------------------------------------------------------------------
39  
40      private String driverName;
41  
42      private String url;
43  
44      private String userName;
45  
46      private String password;
47  
48      public void setDriverName( String driverName )
49      {
50          this.driverName = driverName;
51      }
52  
53      public void setUrl( String url )
54      {
55          this.url = url;
56      }
57  
58      public void setUserName( String userName )
59      {
60          this.userName = userName;
61      }
62  
63      public void setPassword( String password )
64      {
65          this.password = password;
66      }
67  
68      public void shutdown()
69          throws Exception
70      {
71          if ( url != null )
72          {
73  
74              if ( url.indexOf( "jdbc:derby:" ) == 0 )
75              {
76                  String databasePath = url.substring( "jdbc:derby:".length() );
77  
78                  if ( databasePath.indexOf( ';' ) > 0 )
79                  {
80                      databasePath = databasePath.substring( 0, databasePath.indexOf( ';' ) );
81                  }
82  
83                  Connection connection = null;
84  
85                  try
86                  {
87                      /* shutdown the database */
88                      DriverManager.getConnection( "jdbc:derby:" + databasePath + ";shutdown=true" );
89                  }
90                  catch ( SQLException e )
91                  {
92                      /*
93                       * In Derby, any request to the DriverManager with a shutdown=true attribute raises an exception.
94                       * http://db.apache.org/derby/manuals/reference/sqlj251.html
95                       */
96                  }
97                  finally
98                  {
99                      if ( connection != null )
100                     {
101                         try
102                         {
103                             connection.close();
104                         }
105                         catch ( SQLException e )
106                         {
107                             // ignore
108                         }
109                     }
110                 }
111 
112                 System.gc();
113             }
114         }
115         super.shutdown();
116     }
117 
118     public Properties getProperties()
119     {
120         synchronized ( configured )
121         {
122             if ( configured == Boolean.TRUE )
123             {
124                 return properties;
125             }
126             else
127             {
128                 Properties properties = new Properties();
129 
130                 Iterator it = otherProperties.entrySet().iterator();
131                 while ( it.hasNext() )
132                 {
133                     Map.Entry entry = (Map.Entry) it.next();
134                     properties.setProperty( (String) entry.getKey(), (String) entry.getValue() );
135                 }
136 
137                 setPropertyInner( properties, "javax.jdo.PersistenceManagerFactoryClass",
138                                   persistenceManagerFactoryClass );
139                 setPropertyInner( properties, "javax.jdo.option.ConnectionDriverName", driverName );
140                 setPropertyInner( properties, "javax.jdo.option.ConnectionURL", url );
141                 setPropertyInner( properties, "javax.jdo.option.ConnectionUserName", userName );
142                 setPropertyInner( properties, "javax.jdo.option.ConnectionPassword", password );
143 
144                 return properties;
145             }
146         }
147     }
148 
149     public String getDriverName()
150     {
151         return driverName;
152     }
153 
154     public String getPassword()
155     {
156         return password;
157     }
158 
159     public String getUrl()
160     {
161         return url;
162     }
163 
164     public String getUserName()
165     {
166         return userName;
167     }
168 
169 }