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.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
34
35 public abstract class AbstractConfigurableJdoFactory
36 implements ConfigurableJdoFactory
37 {
38
39 private Logger logger = LoggerFactory.getLogger( getClass() );
40
41
42
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
125
126
127
128
129
130
131
132
133
134
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 }