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

1   package org.apache.archiva.redback.common.jdo.test;
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.PreparedStatement;
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  
26  import javax.jdo.PersistenceManager;
27  
28  import org.jpox.AbstractPersistenceManagerFactory;
29  import org.jpox.ClassLoaderResolver;
30  import org.jpox.plugin.ConfigurationElement;
31  import org.jpox.plugin.Extension;
32  import org.jpox.store.rdbms.RDBMSManager;
33  
34  /**
35   * A extension to JPOX store manager that allows counting the SQL queries
36   * 
37   * @author Carlos Sanchez <a href="mailto:carlos@apache.org">
38   */
39  public class StoreManagerDebug
40      extends RDBMSManager
41  {
42      private static int counter;
43  
44      public StoreManagerDebug( ClassLoaderResolver clr, AbstractPersistenceManagerFactory pmf, String userName,
45                                String password )
46      {
47          super( clr, pmf, userName, password );
48      }
49  
50      /**
51       * This method will change JPOX store manager extension so it uses our class instead of whatever is configured in
52       * the plugin.xml
53       * 
54       * @param pmf
55       */
56      public static void setup( AbstractPersistenceManagerFactory pmf )
57      {
58          /* set our own Store Manager to allow counting SQL statements */
59          Extension[] extensions =
60              pmf.getPMFContext().getPluginManager().getExtensionPoint( "org.jpox.store_manager" ).getExtensions();
61          Extension e = extensions[0];
62          for ( ConfigurationElement element : e.getConfigurationElements() )
63          {
64              element.putAttribute( "class-name", StoreManagerDebug.class.getName() );
65          }
66      }
67  
68      /**
69       * Get the currently configured store manager from JPOX. Will fail if
70       * {@link #setup(AbstractPersistenceManagerFactory)} is not called first.
71       * 
72       * @param persistenceManager
73       * @return
74       */
75      public static StoreManagerDebug getConfiguredStoreManager( PersistenceManager persistenceManager )
76      {
77          return (StoreManagerDebug) ( (org.jpox.PersistenceManager) persistenceManager ).getStoreManager();
78      }
79  
80      @Override
81      public int[] executeStatementBatch( String stmt, PreparedStatement ps )
82          throws SQLException
83      {
84          counter++;
85          return super.executeStatementBatch( stmt, ps );
86      }
87  
88      @Override
89      public ResultSet executeStatementQuery( String stmt, PreparedStatement ps )
90          throws SQLException
91      {
92          counter++;
93          return super.executeStatementQuery( stmt, ps );
94      }
95  
96      @Override
97      public int executeStatementUpdate( String stmt, PreparedStatement ps )
98          throws SQLException
99      {
100         counter++;
101         return super.executeStatementUpdate( stmt, ps );
102     }
103 
104     public void resetCounter()
105     {
106         counter = 0;
107     }
108 
109     public int counter()
110     {
111         return counter;
112     }
113 }