1package org.apache.archiva.redback.common.jdo.test;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import java.sql.PreparedStatement;
23import java.sql.ResultSet;
24import java.sql.SQLException;
2526import javax.jdo.PersistenceManager;
2728import org.jpox.AbstractPersistenceManagerFactory;
29import org.jpox.ClassLoaderResolver;
30import org.jpox.plugin.ConfigurationElement;
31import org.jpox.plugin.Extension;
32import org.jpox.store.rdbms.RDBMSManager;
3334/**35 * A extension to JPOX store manager that allows counting the SQL queries36 * 37 * @author Carlos Sanchez <a href="mailto:carlos@apache.org">38 */39publicclassStoreManagerDebug40extends RDBMSManager
41 {
42privatestaticint counter;
4344publicStoreManagerDebug( ClassLoaderResolver clr, AbstractPersistenceManagerFactory pmf, String userName,
45 String password )
46 {
47super( clr, pmf, userName, password );
48 }
4950/**51 * This method will change JPOX store manager extension so it uses our class instead of whatever is configured in52 * the plugin.xml53 * 54 * @param pmf55 */56publicstaticvoid 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];
62for ( ConfigurationElement element : e.getConfigurationElements() )
63 {
64 element.putAttribute( "class-name", StoreManagerDebug.class.getName() );
65 }
66 }
6768/**69 * Get the currently configured store manager from JPOX. Will fail if70 * {@link #setup(AbstractPersistenceManagerFactory)} is not called first.71 * 72 * @param persistenceManager73 * @return74 */75publicstaticStoreManagerDebug getConfiguredStoreManager( PersistenceManager persistenceManager )
76 {
77return (StoreManagerDebug) ( (org.jpox.PersistenceManager) persistenceManager ).getStoreManager();
78 }
7980 @Override
81publicint[] executeStatementBatch( String stmt, PreparedStatement ps )
82throws SQLException
83 {
84 counter++;
85returnsuper.executeStatementBatch( stmt, ps );
86 }
8788 @Override
89public ResultSet executeStatementQuery( String stmt, PreparedStatement ps )
90throws SQLException
91 {
92 counter++;
93returnsuper.executeStatementQuery( stmt, ps );
94 }
9596 @Override
97publicint executeStatementUpdate( String stmt, PreparedStatement ps )
98throws SQLException
99 {
100 counter++;
101returnsuper.executeStatementUpdate( stmt, ps );
102 }
103104publicvoid resetCounter()
105 {
106 counter = 0;
107 }
108109publicint counter()
110 {
111return counter;
112 }
113 }