This project has retired. For details please refer to its Attic page.
UserConfigurableJdoFactory xref
View Javadoc

1   package org.apache.archiva.redback.common.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.apache.archiva.redback.components.jdo.DefaultConfigurableJdoFactory;
23  import org.apache.archiva.redback.configuration.UserConfiguration;
24  import org.apache.commons.lang.StringUtils;
25  import org.codehaus.plexus.interpolation.InterpolationException;
26  import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
27  import org.codehaus.plexus.interpolation.StringSearchInterpolator;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.stereotype.Service;
31  
32  import javax.annotation.PostConstruct;
33  import javax.inject.Inject;
34  import javax.inject.Named;
35  
36  /**
37   * UserConfigurableJdoFactory
38   *
39   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
40   *
41   */
42  @Service( "jdoFactory#users" )
43  public class UserConfigurableJdoFactory
44      extends DefaultConfigurableJdoFactory
45  {
46  
47      private Logger log = LoggerFactory.getLogger( getClass() );
48  
49      @Inject
50      @Named( value = "userConfiguration#default" )
51      private UserConfiguration config;
52  
53      private String getConfigString( String key, String currentValue, String defaultValue )
54      {
55          String valueFromSysProps = System.getProperty( "redback." + key );
56          if (StringUtils.isNotEmpty( valueFromSysProps ))
57          {
58              return valueFromSysProps;
59          }
60          String value = null;
61          if ( StringUtils.isNotEmpty( currentValue ) )
62          {
63              value = config.getString( key, currentValue );
64          }
65          else
66          {
67              value = config.getString( key, defaultValue );
68          }
69          // do some interpolation as we can have some ${plexus.home} etc...
70          StringSearchInterpolator interpolator = new StringSearchInterpolator();
71          interpolator.addValueSource( new PropertiesBasedValueSource( System.getProperties() ) );
72  
73          try
74          {
75              return interpolator.interpolate( value );
76          }
77          catch ( InterpolationException e )
78          {
79              // ignore interpolation issue
80              log.warn( "skip issue during interpolation " + e.getMessage() );
81              return value;
82          }
83      }
84  
85      @PostConstruct
86      public void initialize()
87      {
88          String jdbcDriverName =
89              getConfigString( "jdbc.driver.name", super.getDriverName(), "org.apache.derby.jdbc.EmbeddedDriver" );
90          String jdbcUrl =
91              getConfigString( "jdbc.url", super.getUrl(), "jdbc:derby:${plexus.home}/database;create=true" );
92  
93          String jdbcUsername = getConfigString( "jdbc.username", super.getUserName(), "sa" );
94          String jdbcPassword = getConfigString( "jdbc.password", super.getPassword(), "" );
95  
96          super.setDriverName( jdbcDriverName );
97          super.setUrl( jdbcUrl );
98          super.setUserName( jdbcUsername );
99          super.setPassword( jdbcPassword );
100 
101         if ( StringUtils.isEmpty( super.persistenceManagerFactoryClass ) )
102         {
103             super.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" );
104         }
105 
106         if ( ( super.otherProperties == null ) || super.otherProperties.isEmpty() )
107         {
108             super.setProperty( "org.jpox.autoCreateSchema", "true" );
109             super.setProperty( "org.jpox.validateSchema", "false" );
110             super.setProperty( "org.jpox.validateTables", "false" );
111             super.setProperty( "org.jpox.validateConstraints", "false" );
112             super.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" );
113             super.setProperty( "org.jpox.rdbms.dateTimezone", "JDK_DEFAULT_TIMEZONE" );
114         }
115 
116         super.initialize();
117     }
118 
119     public UserConfiguration getConfig()
120     {
121         return config;
122     }
123 
124     public void setConfig( UserConfiguration config )
125     {
126         this.config = config;
127     }
128 }