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.jdo.JDOHelper;
27  import javax.jdo.PersistenceManagerFactory;
28  import java.util.Iterator;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  /**
33   * AbstractConfigurableJdoFactory
34   */
35  public abstract class AbstractConfigurableJdoFactory
36      implements ConfigurableJdoFactory
37  {
38  
39      private Logger logger = LoggerFactory.getLogger( getClass() );
40  
41      /**
42       * default-value="org.jpox.PersistenceManagerFactoryImpl"
43       */
44      protected String persistenceManagerFactoryClass;
45  
46      protected Boolean configured = Boolean.FALSE;
47  
48      protected Properties properties;
49  
50      private PersistenceManagerFactory pmf;
51  
52      protected Properties otherProperties;
53  
54      @PostConstruct
55      public void initialize()
56      {
57          if ( otherProperties == null )
58          {
59              otherProperties = new Properties();
60          }
61      }
62  
63      public PersistenceManagerFactory getPersistenceManagerFactory()
64      {
65          if ( configured == Boolean.FALSE )
66          {
67              configure();
68          }
69  
70          return pmf;
71      }
72  
73      public void shutdown()
74          throws Exception
75      {
76      }
77  
78      public void setPersistenceManagerFactoryClass( String persistenceManagerFactoryClass )
79      {
80          this.persistenceManagerFactoryClass = persistenceManagerFactoryClass;
81      }
82  
83      public void setProperty( String key, String value )
84      {
85          if ( otherProperties == null )
86          {
87              otherProperties = new Properties();
88          }
89  
90          setPropertyInner( otherProperties, key, value );
91      }
92  
93      public abstract Properties getProperties();
94  
95      private void configure()
96      {
97          synchronized ( configured )
98          {
99              if ( configured == Boolean.TRUE )
100             {
101                 return;
102             }
103 
104             doConfigure();
105         }
106     }
107 
108     private void doConfigure()
109     {
110         Properties properties = getProperties();
111 
112         if ( logger.isDebugEnabled() )
113         {
114             logger.debug( "Configuring JDO Factory." );
115 
116             for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
117             {
118                 Map.Entry entry = (Map.Entry) it.next();
119 
120                 logger.debug( "{}={}", entry.getKey(), entry.getValue() );
121             }
122         }
123 
124         /* TODO: implement these
125         javax.jdo.option.IgnoreCache	true | false	Whether to ignore the cache for queries
126         javax.jdo.option.Multithreaded	true | false	Whether to run the PersistenceManager multithreaded
127         javax.jdo.option.NontransactionalRead	true | false	Whether to allow nontransactional reads
128         javax.jdo.option.NontransactionalWrite	true | false	Whether to allow nontransactional writes. Not supported by JPOX
129         javax.jdo.option.Optimistic	true | false	Whether to use Optimistic transactions
130         javax.jdo.option.RetainValues	true | false	Whether to suppress automatic eviction of persistent instances on transaction completion
131         javax.jdo.option.RestoreValues	true | false	Whether persistent object have transactional field values restored when transaction rollback occurs.
132         javax.jdo.option.Mapping		Name for the ORM MetaData mapping files to use with this PMF. For example if this is set to "mysql" then the implementation looks for MetaData mapping files called "'classname'-mysql.orm" or "package-mysql.orm". If this is not specified then the JDO implementation assumes that all is specified in the JDO MetaData file.
133         javax.jdo.mapping.Catalog		Name of the catalog to use by default for all classes persisted using this PMF. This can be overridden in the MetaData where required, and is optional. JPOX will prefix all table names with this catalog name if the RDBMS supports specification of catalog names in DDL.
134         javax.jdo.mapping.Schema		Name of the schema to use by default for all classes persisted using this PMF. This can be overridden in the MetaData where required, and is optional. JPOX will prefix all table names with this schema name if the RDBMS supports specification of schema names in DDL.
135         */
136 
137         pmf = JDOHelper.getPersistenceManagerFactory( properties );
138 
139         this.properties = properties;
140 
141         configured = Boolean.TRUE;
142     }
143 
144     protected void setPropertyInner( Properties properties, String key, String value )
145     {
146         if ( key == null )
147         {
148             throw new IllegalArgumentException( "The key cannot be null." );
149         }
150 
151         if ( value == null )
152         {
153             return;
154         }
155 
156         properties.setProperty( key, value );
157     }
158 
159     public Boolean getConfigured()
160     {
161         return configured;
162     }
163 
164     public void setConfigured( Boolean configured )
165     {
166         this.configured = configured;
167     }
168 
169     public Properties getOtherProperties()
170     {
171         return otherProperties;
172     }
173 
174     public void setOtherProperties( Properties otherProperties )
175     {
176         this.otherProperties = otherProperties;
177     }
178 }