View Javadoc
1   package org.apache.fulcrum.security.torque.peer;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.util.Map;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import org.apache.avalon.framework.activity.Disposable;
24  import org.apache.fulcrum.security.spi.AbstractManager;
25  import org.apache.fulcrum.security.util.DataBackendException;
26  
27  /**
28   * Use this class, if you want to replace the default Torque Peer classes with your own. 
29   *  
30   * To use it, the PeerImpl classes (usually generated) must implement 
31   * at least the {@linkplain Peer} marker interface or some extended interface.
32   * 
33   * @see PeerManager
34   * 
35   * @author <a href="mailto:gk@apache.org">Georg Kallidis</a>
36   * @version $Id$
37   * 
38   */
39  public class PeerManagerDefaultImpl extends AbstractManager
40      implements PeerManager, Disposable
41  {
42        
43  	 /** Serial version */
44  	private static final long serialVersionUID = -3891813089694207441L;
45  	private Map<String,Peer> peers = new ConcurrentHashMap<String,Peer>(4,0.75f,4);
46  
47      /* (non-Javadoc)
48       * @see org.apache.fulcrum.security.torque.peer.PeerManager#getPeerInstance(java.lang.String)
49       */
50      @Override
51      public <P extends Peer> P getPeerInstance(String peerClassName) throws DataBackendException
52      {
53          return getPeerInstance( peerClassName, Peer.class, null);
54      }
55  
56      /* (non-Javadoc)
57       * @see org.apache.fulcrum.security.torque.peer.PeerManager#getPeerInstance(java.lang.String, java.lang.Class, java.lang.String)
58       */
59      @SuppressWarnings( "unchecked" )
60      @Override
61      public <P extends Peer> P getPeerInstance( String peerClassName, Class<? extends Peer> peerInterface , String className) throws DataBackendException
62      {
63          if (peers.containsKey(peerClassName )) {
64              getLogger().debug( " get cached PeerInstance():" +  peers.get( peerClassName ));
65              return (P) peers.get( peerClassName );
66          }
67          try
68          {
69              P peer = (P) Class.forName(peerClassName).getConstructor().newInstance();
70              getLogger().debug( " getPeerInstance():" +  peer);
71              peers.put( peerClassName, peer );
72              return peer;
73          }
74          catch (ClassCastException e) {
75              throw new DataBackendException( e.getMessage()+ ".\nThe peer class " + peerClassName + " should implement "+ peerInterface + "\n of generic type <"+className +">.",e );
76          } 
77          catch (Throwable e)
78          {
79              throw new DataBackendException("Problem creating instance of class " + peerClassName, e);
80          }             
81      }
82  
83  
84  }