Return-Path: Delivered-To: apmail-labs-commits-archive@locus.apache.org Received: (qmail 72826 invoked from network); 27 Jul 2008 21:13:30 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 27 Jul 2008 21:13:30 -0000 Received: (qmail 14735 invoked by uid 500); 27 Jul 2008 21:13:29 -0000 Delivered-To: apmail-labs-commits-archive@labs.apache.org Received: (qmail 14626 invoked by uid 500); 27 Jul 2008 21:13:29 -0000 Mailing-List: contact commits-help@labs.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: labs@labs.apache.org Delivered-To: mailing list commits@labs.apache.org Received: (qmail 14617 invoked by uid 99); 27 Jul 2008 21:13:29 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Jul 2008 14:13:29 -0700 X-ASF-Spam-Status: No, hits=-1999.3 required=10.0 tests=ALL_TRUSTED,FRT_LEVITRA X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 27 Jul 2008 21:12:41 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 448642388A50; Sun, 27 Jul 2008 14:13:07 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r680187 [5/6] - in /labs/jaxmas/trunk/JaxMas: ./ .settings/ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/labs/ src/main/java/org/apache/labs/jaxmas/ src/main/java/org/apache/labs/jaxmas... Date: Sun, 27 Jul 2008 21:13:03 -0000 To: commits@labs.apache.org From: jochen@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080727211307.448642388A50@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/HsqlDbDriver.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/HsqlDbDriver.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/HsqlDbDriver.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/HsqlDbDriver.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; + +import javax.xml.registry.JAXRException; +import javax.xml.registry.RegistryService; + + +/** + * Implementation of {@link DbDriver} for HsqlDb. + */ +public class HsqlDbDriver extends AbstractDbDriver { + /** + * Creates a new instance. + */ + public HsqlDbDriver(RegistryService pRegistryService) { + super(pRegistryService); + } + + @Override + public void shutdown() throws JAXRException { + final String s = "SHUTDOWN"; //$NON-NLS-1$ + Sql.run(getRegistryService(), s); + } + + @Override + public boolean isUnknownTableError(SQLException pException) { + if ("S0002".equals(pException.getSQLState()) && pException.getErrorCode() == -22) { //$NON-NLS-1$ + return true; + } + return false; + } + + @Override + public String getDbProperty(String pProperty) { + if ("CLOB".equals(pProperty)) { //$NON-NLS-1$ + return "LONGVARCHAR"; //$NON-NLS-1$ + } + throw new IllegalArgumentException("Unknown property name: " + pProperty); //$NON-NLS-1$ + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/KeyPredicate.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/KeyPredicate.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/KeyPredicate.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/KeyPredicate.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.List; + +import javax.xml.registry.JAXRException; + + +/** + * A predicate for the registry objects key. + */ +public class KeyPredicate implements Predicate { + private final Collection keys; + + /** + * Creates a new instance with the given keys. + */ + public KeyPredicate(Collection pKeys) { + keys = pKeys; + } + + @Override + public void add(StringBuilder pBuffer, List pParameters) + throws SQLException, JAXRException { + if (keys == null || keys.size() == 0) { + pBuffer.append("1=1"); //$NON-NLS-1$ + return; + } + if (keys.size() == 1) { + pBuffer.append("ro.roKey=?"); //$NON-NLS-1$ + pParameters.add(keys.iterator().next()); + } else { + pBuffer.append("ro.roKey IN ("); //$NON-NLS-1$ + boolean first = true; + for (Object o : keys) { + if (first) { + first = false; + pBuffer.append("?"); //$NON-NLS-1$ + } else { + pBuffer.append(",?"); //$NON-NLS-1$ + } + pParameters.add(o); + } + pBuffer.append(")"); //$NON-NLS-1$ + } + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/NamePredicate.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/NamePredicate.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/NamePredicate.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/NamePredicate.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.List; + +import javax.xml.registry.JAXRException; + +import org.apache.labs.jaxmas.registry.infomodel.InternationalStringImpl; + + +/** + * A predicate for the registry objects name. + */ +public class NamePredicate implements Predicate { + private final Collection namePatterns; + + /** + * Creates a new instance with the given name patterns. + */ + public NamePredicate(Collection pNamePatterns) { + namePatterns = pNamePatterns; + } + + @Override + public void add(StringBuilder pBuffer, List pParameters) + throws SQLException, JAXRException { + if (namePatterns == null || namePatterns.size() == 0) { + pBuffer.append("1=1"); //$NON-NLS-1$ + return; + } + pBuffer.append("ro.roKey IN (SELECT roKey FROM LocalizedStrings WHERE lsType="); //$NON-NLS-1$ + pBuffer.append(InternationalStringImpl.Type.name.ordinal()); + if (pParameters.size() == 1) { + pBuffer.append(" AND val LIKE ?"); //$NON-NLS-1$ + pParameters.add(namePatterns.iterator().next()); + } else { + pBuffer.append(" AND ("); //$NON-NLS-1$ + boolean first = true; + for (String namePattern : namePatterns) { + if (first) { + first = false; + } else { + pBuffer.append(" OR "); //$NON-NLS-1$ + } + pBuffer.append("val LIKE ?"); //$NON-NLS-1$ + pParameters.add(namePattern); + } + pBuffer.append(")"); //$NON-NLS-1$ + } + pBuffer.append(")"); //$NON-NLS-1$ + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjConnUser.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjConnUser.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjConnUser.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjConnUser.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + + +/** + * Implementation of {@link ConnUser} for database operations, + * which perform no result, or an object valued result. + */ +public abstract class ObjConnUser extends ConnUser { + // No additional methods, or fields. +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjQueryUser.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjQueryUser.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjQueryUser.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjQueryUser.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + + +/** + * Extension of {@link QueryUser} for statements, which produce + * no result, or an object values result. + */ +public abstract class ObjQueryUser extends QueryUser { + /** + * Creates a new instance, which performs the given statement + * using the given parameters. + */ + public ObjQueryUser(String pStatement, Object... pParams) { + super(pStatement, pParams); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjStmtUser.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjStmtUser.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjStmtUser.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/ObjStmtUser.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + + + +/** + * Implementation of {@link StmtUser} for database operations, + * which perform no result, or an object valued result. + */ +public abstract class ObjStmtUser extends StmtUser { + /** + * Creates a new instance with the given statement and parameters. + */ + public ObjStmtUser(String pStatement, Object... pParams) { + super(pStatement, pParams); + } + + /** + * Creates a new instance with the given statement and no parameters. + */ + public ObjStmtUser(String pStatement) { + super(pStatement); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/OwnerPredicate.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/OwnerPredicate.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/OwnerPredicate.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/OwnerPredicate.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; +import java.util.List; + +import javax.xml.registry.JAXRException; +import javax.xml.registry.infomodel.Key; + + +/** + * A predicate for the objects owner. + */ +public class OwnerPredicate implements Predicate { + private final Object key; + + /** + * Creates a new instance with the given owners key. + */ + public OwnerPredicate(Key pKey) { + assert(pKey != null); + key = pKey; + } + + /** + * Creates a new instance with the given owners key. + */ + public OwnerPredicate(String pKey) { + assert(pKey != null); + key = pKey; + } + + @Override + public void add(StringBuilder pBuffer, List pParameters) throws SQLException, JAXRException { + pBuffer.append("ro.roOwner=?"); //$NON-NLS-1$ + pParameters.add(key); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Predicate.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Predicate.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Predicate.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Predicate.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; +import java.util.List; + +import javax.xml.registry.JAXRException; + + +/** + * A predicate is a constraint for searching registry objects. + */ +public interface Predicate { + /** + * Converts the predicate into a boolean SQL condition. + */ + void add(StringBuilder pBuffer, List pParameters) throws SQLException, JAXRException; +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/QueryUser.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/QueryUser.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/QueryUser.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/QueryUser.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.xml.registry.JAXRException; + + +/** + * Extension of {@link StmtUser} for database operations, which are + * reading a query result. + */ +public abstract class QueryUser extends StmtUser { + /** + * Creates a new instance, which performs the given statement + * using the given parameters. + */ + public QueryUser(String pStatement, Object... pParams) { + super(pStatement, pParams); + } + + /** + * Immplemented by the concrete subclass for actually reading the + * query result. + */ + protected abstract void action(ResultSet pResultSet) throws JAXRException, SQLException; + + @Override + protected void action(PreparedStatement pStatement) throws JAXRException, SQLException { + ResultSet rs = pStatement.executeQuery(); + try { + action(rs); + rs.close(); + rs = null; + } finally { + if (rs != null) { try { rs.close(); } catch (Throwable t) { /* Ignore me */ } } + } + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectLoader.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectLoader.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectLoader.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectLoader.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,562 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import javax.xml.registry.JAXRException; +import javax.xml.registry.RegistryService; +import javax.xml.registry.infomodel.Association; +import javax.xml.registry.infomodel.Classification; +import javax.xml.registry.infomodel.Concept; +import javax.xml.registry.infomodel.RegistryEntry; +import javax.xml.registry.infomodel.RegistryObject; + +import org.apache.labs.jaxmas.registry.accessor.ROAccessor; +import org.apache.labs.jaxmas.registry.accessor.ROAccessors; +import org.apache.labs.jaxmas.registry.infomodel.AssociationImpl; +import org.apache.labs.jaxmas.registry.infomodel.ClassificationImpl; +import org.apache.labs.jaxmas.registry.infomodel.KeyImpl; +import org.apache.labs.jaxmas.registry.infomodel.OwnedRegistryObject; +import org.apache.labs.jaxmas.registry.infomodel.ROState; +import org.apache.labs.jaxmas.registry.infomodel.RegistryEntryImpl; +import org.apache.labs.jaxmas.registry.infomodel.RegistryObjectImpl; + + +/** + * An instance of this class is used for loading registry + * objects. + */ +public class RegistryObjectLoader { + private static final ExtensionHandler[] EXTENSION_HANDLERS_ASSOCIATION = new ExtensionHandler[]{ + new ClassificationExtensionHandler(ROAccessors.ObjectTypes.ASSOCIATION) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_CLASSIFICATION = new ExtensionHandler[]{ + new ClassificationExtensionHandler(ROAccessors.ObjectTypes.CLASSIFICATION) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_EXTRINSIC_OBJECT = new ExtensionHandler[]{ + new RegistryEntryExtensionHandler(ROAccessors.ObjectTypes.EXTRINSIC_OBJECT) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_SERVICE = new ExtensionHandler[]{ + new RegistryEntryExtensionHandler(ROAccessors.ObjectTypes.SERVICE) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_REGISTRY_PACKAGE = new ExtensionHandler[]{ + new RegistryEntryExtensionHandler(ROAccessors.ObjectTypes.REGISTRY_PACKAGE) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_REGISTRY_ENTRY = new ExtensionHandler[]{ + new RegistryEntryExtensionHandler(ROAccessors.ObjectTypes.REGISTRY_ENTRY) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_CLASSIFICATION_SCHEME = new ExtensionHandler[]{ + new RegistryEntryExtensionHandler(ROAccessors.ObjectTypes.CLASSIFICATION_SCHEME) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_CONCEPT = new ExtensionHandler[]{ + new ConceptExtensionHandler(ROAccessors.ObjectTypes.CONCEPT) + }; + private static final ExtensionHandler[] EXTENSION_HANDLERS_NULL_TYPE = new ExtensionHandler[]{ + new ConceptExtensionHandler(null), + new RegistryEntryExtensionHandler(null), + new AssociationExtensionHandler(null), + new ClassificationExtensionHandler(null) + }; + + /** + * Extension handlers are used for type specific extension tables. + */ + public static abstract class ExtensionHandler { + private final ROAccessors.ObjectTypes objectType; + + /** + * Creates a new instance with the given object type. + */ + public ExtensionHandler(ROAccessors.ObjectTypes pObjectType) { + objectType = pObjectType; + } + + /** + * Returns the extension handlers object type. May be null, if the + * object type is not specified. + */ + protected ROAccessors.ObjectTypes getObjectType() { + return objectType; + } + + /** + * Called to extend the column list of the SQL query for reading registry objects. + */ + protected abstract int addColumns(StringBuilder pBuilder, int pIndex); + /** + * Called to extend the FROM clause of the SQL query for reading registry objects. + */ + protected abstract void addJoin(StringBuilder pBuilder, List pParameters); + /** + * Called to pull data from the resul set when reading registry objects. + */ + protected abstract void load(ResultSet pResultSet, RegistryObject pRegistryObject, int pIndex) + throws SQLException, JAXRException; + } + + /** + * Extension handler for concepts. + */ + public static class ConceptExtensionHandler extends ExtensionHandler { + /** + * Creates a new instance with the given object type. + */ + public ConceptExtensionHandler(ROAccessors.ObjectTypes pObjectType) { + super(pObjectType); + } + + @Override + protected int addColumns(StringBuilder pBuilder, int pIndex) { + pBuilder.append(", c.value"); //$NON-NLS-1$ + return pIndex + 1; + } + + @Override + protected void addJoin(StringBuilder pBuilder, List pParameters) { + if (getObjectType() == null) { + pBuilder.append(" LEFT OUTER"); //$NON-NLS-1$ + } + pBuilder.append(" JOIN Concepts c ON ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(ROAccessors.ObjectTypes.CONCEPT.ordinal())); + pBuilder.append(" AND ro.roKey=c.roKey"); //$NON-NLS-1$ + } + + @Override + protected void load(final ResultSet pResultSet, final RegistryObject pRegistryObject, int pIndex) + throws SQLException, JAXRException { + if (pRegistryObject instanceof Concept) { + final Concept concept = (Concept) pRegistryObject; + concept.setValue(pResultSet.getString(pIndex)); + } + } + } + + /** + * Extension handler for registry entries. + */ + public static class RegistryEntryExtensionHandler extends ExtensionHandler { + /** + * Creates a new instance with the given object type. + */ + public RegistryEntryExtensionHandler(ROAccessors.ObjectTypes pObjectType) { + super(pObjectType); + } + + @Override + protected int addColumns(StringBuilder pBuilder, int pIndex) { + pBuilder.append(", re.expiration, re.status, re.stability, re.majorVersion, re.minorVersion, re.userVersion"); //$NON-NLS-1$ + return pIndex + 6; + } + + @Override + protected void addJoin(StringBuilder pBuilder, List pParameters) { + final ROAccessors.ObjectTypes objectType = getObjectType(); + if (objectType == null) { + pBuilder.append(" LEFT OUTER JOIN RegistryEntries re ON ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(ROAccessors.ObjectTypes.CLASSIFICATION_SCHEME.ordinal())); + pBuilder.append(" OR ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(ROAccessors.ObjectTypes.REGISTRY_ENTRY.ordinal())); + } else { + pBuilder.append(" JOIN RegistryEntries re ON ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(objectType.ordinal())); + } + pBuilder.append(" AND ro.roKey=re.roKey"); //$NON-NLS-1$ + } + + @Override + protected void load(final ResultSet pResultSet, final RegistryObject pRegistryObject, int pIndex) + throws SQLException, JAXRException { + if (pRegistryObject instanceof RegistryEntry) { + final RegistryEntryImpl re = (RegistryEntryImpl) pRegistryObject; + re.setExpiration(pResultSet.getDate(pIndex)); + re.setStability(pResultSet.getInt(pIndex+2)); + re.setMajorVersion(pResultSet.getInt(pIndex+3)); + re.setMinorVersion(pResultSet.getInt(pIndex+4)); + re.setUserVersion(pResultSet.getString(pIndex+5)); + } + } + } + + /** + * Extension handler for associations. + */ + public static class AssociationExtensionHandler extends ExtensionHandler { + /** + * Creates a new instance with the given object type. + */ + public AssociationExtensionHandler(ROAccessors.ObjectTypes pObjectType) { + super(pObjectType); + } + + @Override + protected int addColumns(StringBuilder pBuilder, int pIndex) { + pBuilder.append(", assoc.roKeyTarget, assoc.roKeyType"); //$NON-NLS-1$ + return pIndex + 2; + } + + @Override + protected void addJoin(StringBuilder pBuilder, List pParameters) { + if (getObjectType() == null) { + pBuilder.append(" LEFT OUTER"); //$NON-NLS-1$ + } + pBuilder.append(" JOIN Associations assoc ON ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(ROAccessors.ObjectTypes.ASSOCIATION.ordinal())); + pBuilder.append(" AND ro.roKey=assoc.roKey"); //$NON-NLS-1$ + } + + @Override + protected void load(final ResultSet pResultSet, final RegistryObject pRegistryObject, int pIndex) + throws SQLException, JAXRException { + if (pRegistryObject instanceof Association) { + final AssociationImpl assoc = (AssociationImpl) pRegistryObject; + final String targetKey = pResultSet.getString(pIndex); + assert(targetKey != null); + assoc.setTargetObjectKey(new KeyImpl(targetKey)); + final String typeKey = pResultSet.getString(pIndex+1); + assert(typeKey != null); + assoc.setAssociationTypeKey(new KeyImpl(typeKey)); + } + } + } + + /** + * Extension handler for classifications. + */ + public static class ClassificationExtensionHandler extends ExtensionHandler { + /** + * Creates a new instance with the given object type. + */ + public ClassificationExtensionHandler(ROAccessors.ObjectTypes pObjectType) { + super(pObjectType); + } + + @Override + protected int addColumns(StringBuilder pBuilder, int pIndex) { + pBuilder.append(", cl.roKeyConcept"); //$NON-NLS-1$ + return pIndex + 1; + } + + @Override + protected void addJoin(StringBuilder pBuilder, List pParameters) { + if (getObjectType() == null) { + pBuilder.append(" LEFT OUTER"); //$NON-NLS-1$ + } + pBuilder.append(" JOIN Classifications cl ON ro.roType="); //$NON-NLS-1$ + pBuilder.append(String.valueOf(ROAccessors.ObjectTypes.CLASSIFICATION.ordinal())); + pBuilder.append(" AND ro.roKey=cl.roKey"); //$NON-NLS-1$ + } + + @Override + protected void load(final ResultSet pResultSet, final RegistryObject pRegistryObject, int pIndex) + throws SQLException, JAXRException { + if (pRegistryObject instanceof Classification) { + final ClassificationImpl cl = (ClassificationImpl) pRegistryObject; + final String conceptKey = pResultSet.getString(pIndex); + assert(conceptKey != null); + cl.setConceptKey(new KeyImpl(conceptKey)); + } + } + } + + private final String query; + private int numPredicates; + private final Object[] parameters; + private final ROAccessors.ObjectTypes objectType; + private final ExtensionHandler[] extensionHandlers; + private final int[] indexes; + + private static ROAccessors.ObjectTypes getObjectType(ROAccessors.ObjectTypes pObjectType, + Collection pPredicates) { + ROAccessors.ObjectTypes type = pObjectType; + if (pPredicates != null) { + for (Predicate predicate : pPredicates) { + if (predicate instanceof RegistryObjectTypePredicate) { + ROAccessors.ObjectTypes predicateType = ((RegistryObjectTypePredicate) predicate).getType(); + if (type == null) { + type = predicateType; + } else if (type != predicateType) { + throw new IllegalStateException("Different object types specified."); //$NON-NLS-1$ + } + } + } + } + return type; + } + + /** + * Returns the set of extension handlers. + */ + protected ExtensionHandler[] getExtensionHandlers() { + return extensionHandlers; + } + + private static ExtensionHandler[] getExtensionHandlers(ROAccessors.ObjectTypes pObjectType) { + if (pObjectType == null) { + return EXTENSION_HANDLERS_NULL_TYPE; + } + switch (pObjectType) { + case CONCEPT: + return EXTENSION_HANDLERS_CONCEPT; + case CLASSIFICATION_SCHEME: + return EXTENSION_HANDLERS_CLASSIFICATION_SCHEME; + case REGISTRY_ENTRY: + return EXTENSION_HANDLERS_REGISTRY_ENTRY; + case REGISTRY_PACKAGE: + return EXTENSION_HANDLERS_REGISTRY_PACKAGE; + case SERVICE: + return EXTENSION_HANDLERS_SERVICE; + case EXTRINSIC_OBJECT: + return EXTENSION_HANDLERS_EXTRINSIC_OBJECT; + case CLASSIFICATION: + return EXTENSION_HANDLERS_CLASSIFICATION; + case ASSOCIATION: + return EXTENSION_HANDLERS_ASSOCIATION; + default: + return null; + } + } + + /** + * Creates a new instance, which is searching for the given object type. + */ + public RegistryObjectLoader(ROAccessors.ObjectTypes pObjectType, Collection pPredicates) + throws JAXRException, SQLException { + objectType = getObjectType(pObjectType, pPredicates); + extensionHandlers = getExtensionHandlers(objectType); + final StringBuilder sb = new StringBuilder(); + sb.append("SELECT ro.roKey, ro.roOwner, ro.pos, ro.roType"); //$NON-NLS-1$ + if (extensionHandlers == null) { + indexes = null; + } else { + indexes = new int[extensionHandlers.length]; + int index = 5; + for (int i = 0; i < extensionHandlers.length; i++) { + indexes[i] = index; + index = extensionHandlers[i].addColumns(sb, index); + } + } + sb.append(" FROM RegistryObjects ro"); //$NON-NLS-1$ + final List parameterList = new ArrayList(); + if (extensionHandlers != null) { + for (int i = 0; i < extensionHandlers.length; i++) { + extensionHandlers[i].addJoin(sb, parameterList); + } + } + if (objectType != null) { + numPredicates++; + sb.append(" WHERE ro.roType=?"); //$NON-NLS-1$ + parameterList.add(objectType); + } + if (pPredicates != null) { + for (Predicate predicate : pPredicates) { + if (predicate instanceof RegistryObjectTypePredicate) { + continue; + } + if (numPredicates++ == 0) { + sb.append(" WHERE "); //$NON-NLS-1$ + } else { + sb.append(" AND "); //$NON-NLS-1$ + } + predicate.add(sb, parameterList); + } + } + parameters = parameterList.toArray(); + query = sb.toString(); + } + + /** + * Creates a new instance with the given predicate list. + */ + public RegistryObjectLoader(Collection pPredicates) + throws SQLException, JAXRException { + this(null, pPredicates); + } + + /** + * Called to read multiple result objects by specifying additional predicates. + */ + public List getResultList(RegistryService pRegistryService, + Collection pPredicates) throws JAXRException { + if (pPredicates == null || pPredicates.size() == 0) { + return getResultList(pRegistryService, query, parameters); + } + final StringBuilder sb = new StringBuilder(query); + final List params = appendToQuery(pPredicates, sb); + return getResultList(pRegistryService, sb.toString(), params.toArray()); + } + + private List appendToQuery(Collection pPredicates, + final StringBuilder sb) throws JAXRException { + final List params = new ArrayList(Arrays.asList(parameters)); + int numPreds = numPredicates; + for (Predicate predicate : pPredicates) { + if (predicate instanceof RegistryObjectTypePredicate) { + if (((RegistryObjectTypePredicate) predicate).getType() == objectType) { + continue; + } + throw new IllegalStateException("A different object type must not be specified."); //$NON-NLS-1$ + } + sb.append((numPreds++ == 0) ? " WHERE " : " AND "); //$NON-NLS-1$ //$NON-NLS-2$ + try { + predicate.add(sb, params); + } catch (SQLException e) { + throw new JAXRException(e); + } + } + return params; + } + + /** + * Called to read multiple result objects. + */ + public List getResultList(RegistryService pRegistryService) + throws JAXRException { + return getResultList(pRegistryService, query, parameters); + } + + private List getResultList(final RegistryService pRegistryService, + String pQuery, Object[] pParameters) throws JAXRException { + final List list = new ArrayList(); + new ObjQueryUser(pQuery, pParameters){ + @Override + protected void action(ResultSet pResultSet) throws JAXRException, + SQLException { + while (pResultSet.next()) { + int type = pResultSet.getInt(4); + assert(!pResultSet.wasNull()); + final ROAccessor roAcc = ROAccessors.getROAccessor(type); + final String key = pResultSet.getString(1); + assert(key != null); + final RegistryObject ro = roAcc.create(pRegistryService, new KeyImpl(key)); + load(ro, pResultSet); + list.add(ro); + } + setResult(Integer.valueOf(list.size())); + } + }.run(pRegistryService); + return list; + } + + /** + * Called to read a single result object by applying additional parameters. + */ + @SuppressWarnings("unchecked") + public RO getResultObject(RegistryService pRegistryService, + Collection pPredicates) throws JAXRException { + final RegistryObject ro; + if (pPredicates == null || pPredicates.isEmpty()) { + ro = getResultObject(pRegistryService, query, parameters); + } else { + final StringBuilder sb = new StringBuilder(query); + final List params = appendToQuery(pPredicates, sb); + ro = getResultObject(pRegistryService, sb.toString(), params.toArray()); + } + return (RO) ro; + } + + /** + * Called to read a single result object. + */ + @SuppressWarnings("unchecked") + public RO getResultObject(RegistryService pRegistryService) + throws JAXRException { + final RegistryObject ro = getResultObject(pRegistryService, query, parameters); + return (RO) ro; + } + + @SuppressWarnings("unchecked") + private ROAccessor asROAccessor(ROAccessor pRO) { + return (ROAccessor) pRO; + } + + private RegistryObject getResultObject(final RegistryService pRegistryService, + String pQuery, Object[] pParameters) throws JAXRException { + final QueryUser qu = new QueryUser(pQuery, pParameters){ + @Override + protected void action(ResultSet pResultSet) throws JAXRException, + SQLException { + if (pResultSet.next()) { + final int type = pResultSet.getInt(4); + assert(!pResultSet.wasNull()); + final ROAccessor roAcc = asROAccessor(ROAccessors.getROAccessor(type)); + final String key = pResultSet.getString(1); + assert(key != null); + final RegistryObject ro = roAcc.create(pRegistryService, new KeyImpl(key)); + load(ro, pResultSet); + setResult(ro); + } + } + }; + qu.run(pRegistryService); + return qu.getResult(); + } + + private void load(RegistryObject pRegistryObject, ResultSet pResultSet) throws SQLException, JAXRException { + final RegistryObjectImpl ro = (RegistryObjectImpl) pRegistryObject; + ro.setState(ROState.loaded); + + if (pRegistryObject instanceof OwnedRegistryObject) { + final OwnedRegistryObject oro = (OwnedRegistryObject) ro; + final String ownerKey = pResultSet.getString(2); + assert(ownerKey != null); + oro.setOwner(new KeyImpl(ownerKey)); + int pos = pResultSet.getInt(3); + assert(!pResultSet.wasNull()); + oro.setPosition(pos); + } + + if (extensionHandlers != null) { + for (int i = 0; i < extensionHandlers.length; i++) { + extensionHandlers[i].load(pResultSet, pRegistryObject, indexes[i]); + } + } + } + + /** + * Called to load the given objects data from the registry. + */ + public void load(RegistryService pRegistryService, final RegistryObject pRegistryObject) + throws JAXRException { + final Predicate predicate = new KeyPredicate(Collections.singleton(pRegistryObject.getKey())); + final StringBuilder sb = new StringBuilder(query); + final List params = appendToQuery(Collections.singleton(predicate), sb); + new ObjQueryUser(sb.toString(), params.toArray()){ + @Override + protected void action(ResultSet pResultSet) throws JAXRException, + SQLException { + if (!pResultSet.next()) { + throw new IllegalStateException("Object not found: " + pRegistryObject.getKey()); //$NON-NLS-1$ + } + final int type = pResultSet.getInt(4); + assert(!pResultSet.wasNull() && type == objectType.ordinal()); + load(pRegistryObject, pResultSet); + if (pResultSet.next()) { + throw new IllegalStateException("Multiple objects found: " + pRegistryObject.getKey()); //$NON-NLS-1$ + } + } + }.run(pRegistryService); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectTypePredicate.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectTypePredicate.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectTypePredicate.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/RegistryObjectTypePredicate.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.SQLException; +import java.util.List; + +import javax.xml.registry.JAXRException; + +import org.apache.labs.jaxmas.registry.accessor.ROAccessors; + + +/** + * Predicate for restricting a search to a particular registry object type. + */ +public class RegistryObjectTypePredicate implements Predicate { + private final ROAccessors.ObjectTypes type; + + /** + * Creates a new instance, which restricts the search to the given + * type. + */ + public RegistryObjectTypePredicate(ROAccessors.ObjectTypes pType) { + type = pType; + } + + /** + * Returns the object type. + */ + public ROAccessors.ObjectTypes getType() { + return type; + } + + @Override + public void add(StringBuilder pBuffer, List pParameters) throws SQLException, JAXRException { + pBuffer.append("ro.roType=?"); //$NON-NLS-1$ + pParameters.add(type); + } + +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Sql.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Sql.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Sql.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/Sql.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.xml.registry.JAXRException; +import javax.xml.registry.RegistryService; + +import org.apache.labs.jaxmas.registry.infomodel.RegistryServiceImpl; + + +/** + * Utility class for performing database operations. + */ +public class Sql { + /** + * Called to perform the given prepared statement with the given parameters. + * @return The result of {@link PreparedStatement#executeUpdate()}. + */ + public static int count(RegistryService pRegistryService, String pStmt, Object... pParams) throws JAXRException { + return count(((RegistryServiceImpl) pRegistryService).getConnection().getConnection(), pStmt, pParams); + } + + /** + * Called to perform the given prepared statement with the given parameters. + * @return The result of {@link PreparedStatement#executeUpdate()}. + */ + public static int count(Connection pConnection, String pStmt, Object... pParams) throws JAXRException { + final StmtUser stmtUser = new StmtUser(pStmt, pParams){ + @Override + protected void action(PreparedStatement pStatement) throws JAXRException, SQLException { + setResult(new Integer(pStatement.executeUpdate())); + } + }; + stmtUser.run(pConnection); + return stmtUser.getResult().intValue(); + } + + /** + * Called to perform the given prepared statement with the given parameters. + * @return The result of {@link PreparedStatement#executeQuery()}. + */ + public static int intQuery(RegistryService pRegistryService, String pStmt, Object... pParams) throws JAXRException { + return intQuery(((RegistryServiceImpl) pRegistryService).getConnection().getConnection(), pStmt, pParams); + } + + /** + * Called to perform the given prepared statement with the given parameters. + * @return The result of {@link PreparedStatement#executeQuery()}. + */ + public static int intQuery(Connection pConnection, String pStmt, Object... pParams) throws JAXRException { + final QueryUser queryUser = new QueryUser(pStmt, pParams){ + @Override + protected void action(ResultSet pResultSet) throws JAXRException, SQLException { + boolean hasResult = pResultSet.next(); + assert(hasResult); + int i = pResultSet.getInt(1); + setResult(new Integer(i)); + assert(!pResultSet.wasNull()); + hasResult = pResultSet.next(); + assert(!hasResult); + } + }; + queryUser.run(pConnection); + return queryUser.getResult().intValue(); + } + + /** + * Called to perform the given prepared statement with the given parameters. + */ + public static void run(RegistryService pRegistryService, String pStmt, Object... pParams) throws JAXRException { + run(((RegistryServiceImpl) pRegistryService).getConnection().getConnection(), pStmt, pParams); + } + + /** + * Called to perform the given prepared statement with the given parameters. + */ + public static void run(Connection pConnection, String pStmt, Object... pParams) throws JAXRException { + new ObjStmtUser(pStmt, pParams){ + @Override + protected void action(PreparedStatement pStatement) throws JAXRException, SQLException { + pStatement.executeUpdate(); + } + }.run(pConnection); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/StmtUser.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/StmtUser.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/StmtUser.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/sql/StmtUser.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,133 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.sql; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; +import java.util.Calendar; +import java.util.Date; + +import javax.xml.registry.JAXRException; +import javax.xml.registry.infomodel.Key; + +import org.apache.labs.jaxmas.registry.util.Calendars; +import org.apache.labs.jaxmas.registry.util.Logger; + + + +/** + * Extension of {@link ConnUser} for database operations, which are + * executing a prepared statement. + */ +public abstract class StmtUser extends ConnUser { + private final String statement; + private Object[] params; + + /** + * Creates a new instance with the given statement and parameters. + */ + public StmtUser(String pStatement, Object... pParams) { + statement = pStatement; + params = pParams; + } + + /** + * Creates a new instance with the given statement and no parameters. + */ + public StmtUser(String pStatement) { + statement = pStatement; + params = null; + } + + /** + * This method must be implemented to perform the actual operation. + * It is called from within {@link #run(Connection)}. + */ + protected abstract void action(java.sql.PreparedStatement pStatement) throws JAXRException, SQLException; + + /** + * Called to prepare a statement by setting the parameters. + */ + protected void setParams(PreparedStatement pStmt, Object[] pParams) throws SQLException, JAXRException { + if (pParams != null) { + for (int i = 0; i < pParams.length; i++) { + Object o = pParams[i]; + if (o == null) { + pStmt.setNull(i+1, Types.VARCHAR); + } else if (o instanceof String) { + pStmt.setString(i+1, (String) o); + } else if (o instanceof Integer) { + pStmt.setInt(i+1, ((Integer) o).intValue()); + } else if (o instanceof Long) { + pStmt.setLong(i+1, ((Long) o).longValue()); + } else if (o instanceof Boolean) { + pStmt.setBoolean(i+1, ((Boolean) o).booleanValue()); + } else if (o instanceof Short) { + pStmt.setShort(i+1, ((Short) o).shortValue()); + } else if (o instanceof Byte) { + pStmt.setByte(i+1, ((Byte) o).byteValue()); + } else if (o instanceof Float) { + pStmt.setFloat(i+1, ((Float) o).floatValue()); + } else if (o instanceof Double) { + pStmt.setDouble(i+1, ((Double) o).doubleValue()); + } else if (o instanceof Date) { + final Calendar cal = Calendars.nowUTC(); + final Timestamp ts = (o instanceof Timestamp) ? ((Timestamp) o) : new Timestamp(((Date) o).getTime()); + pStmt.setTimestamp(i+1, ts, cal); + } else if (o instanceof Key) { + pStmt.setString(i+1, ((Key) o).getId()); + } else if (o instanceof Enum) { + pStmt.setInt(i+1, ((Enum) o).ordinal()); + } else { + throw new IllegalStateException("Invalid parameter type: " + o.getClass().getName()); //$NON-NLS-1$ + } + } + } + } + + /** + * Returns the query parameters. + */ + protected Object[] getParams() { + return params; + } + + @Override + protected void logEntering(Logger pLog, String pName) { + if (pLog.isDebugEnabled()) { + pLog.entering(pName, statement, getParams()); + } + } + + @Override + protected void action(Connection pConnection) throws JAXRException, SQLException { + PreparedStatement stmt = pConnection.prepareStatement(statement); + try { + setParams(stmt, getParams()); + action(stmt); + stmt.close(); + stmt = null; + } finally { + if (stmt != null) { try { stmt.close(); } catch (Throwable t) { /* Ignore me */ } } + } + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Calendars.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Calendars.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Calendars.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Calendars.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + +import java.util.Calendar; +import java.util.TimeZone; + +/** + * Utility class for working with calendars. + */ +public class Calendars { + private static TimeZone GMT = TimeZone.getTimeZone("GMT"); //$NON-NLS-1$ + + /** + * Private constructor, to prevent accidental instantiation. + */ + private Calendars() { + // Does nothing + } + + /** + * Returns a calendar with the current date/time and the UTC time zone. + */ + public static Calendar nowUTC() { + return Calendar.getInstance(GMT); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLogger.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLogger.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLogger.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLogger.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,163 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + +import java.text.DateFormat; + + +/** + * Default implementation of {@link Logger}, which is writing to {@link System#err}. + */ +public class DefaultLogger implements Logger { + /** + * The trace level. + */ + public static final int LEVEL_TRACE = 5; + /** + * The debug level. + */ + public static final int LEVEL_DEBUG = 4; + /** + * The info level. + */ + public static final int LEVEL_INFO = 3; + /** + * The warning level. + */ + public static final int LEVEL_WARN = 2; + /** + * The error level. + */ + public static final int LEVEL_ERR = 1; + /** + * A log level, which suppresses logging at all. + */ + public static final int LEVEL_NONE = 0; + + private static final String DEBUG = "DEBUG"; //$NON-NLS-1$ + private static final String ERROR = "ERROR"; //$NON-NLS-1$ + + private final DateFormat df = DateFormat.getDateTimeInstance(); + private final String id; + private int level; + + /** + * Creates a new instance. + */ + public DefaultLogger(Class pClass, int pLevel) { + this(pClass.getCanonicalName(), pLevel); + } + + /** + * Creates a new instance. + */ + public DefaultLogger(String pId, int pLevel) { + id = pId; + level = pLevel; + } + + @Override + public boolean isDebugEnabled() { + return level >= LEVEL_DEBUG; + } + + @Override + public boolean isErrorEnabled() { + return level >= LEVEL_ERR; + } + + private void asString(StringBuilder pSb, Object[] pArgs) { + if (pArgs != null) { + for (int i = 0; i < pArgs.length; i++) { + if (i > 0) { + pSb.append(", "); //$NON-NLS-1$ + } + pSb.append(pArgs[i]); + } + } + } + + private StringBuilder fName(String pLevel, String pName) { + final StringBuilder sb = new StringBuilder(); + synchronized (df) { + sb.append(df.format(new java.util.Date())); + } + sb.append(" ["); //$NON-NLS-1$ + sb.append(pLevel); + sb.append(','); + sb.append(Thread.currentThread().getName()); + sb.append("] "); //$NON-NLS-1$ + sb.append(id); + sb.append(' '); + sb.append(pName); + return sb; + } + + @Override + public void entering(String pName) { + if (isDebugEnabled()) { + System.err.println(fName(DEBUG, pName).append(": ->")); //$NON-NLS-1$ + } + } + + @Override + public void entering(String pName, String pMsg) { + if (isDebugEnabled()) { + System.err.println(fName(DEBUG, pName).append(": -> ").append(pMsg)); //$NON-NLS-1$ + } + } + + @Override + public void entering(String pName, String pMsg, Object... pArgs) { + if (isDebugEnabled()) { + final StringBuilder sb = fName(DEBUG, pName).append(": -> ").append(pMsg); //$NON-NLS-1$ + asString(sb, pArgs); + System.err.println(sb); + } + } + + @Override + public void exiting(String pName) { + if (isDebugEnabled()) { + System.err.println(fName(DEBUG, pName).append(": <-")); //$NON-NLS-1$ + } + } + + @Override + public void exiting(String pName, String pMsg) { + if (isDebugEnabled()) { + System.err.println(fName(DEBUG, pName).append(": <- ").append(pMsg)); //$NON-NLS-1$ + } + } + + @Override + public void debug(String pName, String pMsg) { + if (isDebugEnabled()) { + System.err.println(fName(DEBUG, pName).append(": ").append(pMsg)); //$NON-NLS-1$ + } + } + + @Override + public void error(String pName, Throwable pThrowable) { + if (isErrorEnabled()) { + final String msg = pThrowable.getMessage() == null ? pThrowable.getClass().getName() : pThrowable.getMessage(); + System.err.println(fName(ERROR, pName).append(": ").append(msg)); //$NON-NLS-1$ + } + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLoggerFactory.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLoggerFactory.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLoggerFactory.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/DefaultLoggerFactory.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + + +/** + * This is the default logger factory, which is used only, + * if log4j is not available. + */ +public class DefaultLoggerFactory implements LoggerFactory { + private static LoggerFactory theInstance; + + static { + LoggerFactory lf; + try { + lf = new Log4jLoggerFactory(); + } catch (Throwable t) { + lf = new DefaultLoggerFactory(); + } + theInstance = lf; + } + + /** + * Returns the current logger factory. + */ + public static synchronized LoggerFactory getInstance() { + return theInstance; + } + + /** + * Sets the current logger factory. + */ + public static synchronized void setLoggerFactory(LoggerFactory pFactory) { + theInstance = pFactory; + } + + private int level = DefaultLogger.LEVEL_DEBUG; + + @Override + public Logger newLogger(Class pClass) { + return new DefaultLogger(pClass, level); + } + + @Override + public Logger newLogger(String pId) { + return new DefaultLogger(pId, level); + } + + /** + * Returns the factories log level, which is used to create new loggers. + */ + public int getLevel() { + return level; + } + + /** + * Sets the factories log level, which is used to create new loggers. + */ + public void setLevel(int pLevel) { + level = pLevel; + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Locales.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Locales.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Locales.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Locales.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + +import java.util.Locale; +import java.util.StringTokenizer; + +/** + * Utility class for working with calendars. + */ +public class Locales { + /** + * Private constructor, to prevent accidental instantiation. + */ + private Locales() { + // Does nothing + } + + /** + * Returns the locale with the given identifier. + */ + public static Locale getLocale(String localeProperty) { + try { + final StringTokenizer st = new StringTokenizer(localeProperty, "_"); //$NON-NLS-1$ + if (st.hasMoreTokens()) { + final String lang = st.nextToken(); + if (st.hasMoreTokens()) { + final String country = st.nextToken(); + if (st.hasMoreTokens()) { + final String variant = st.nextToken(); + return new Locale(lang, country, variant); + } + return new Locale(lang, country); + } + return new Locale(lang); + } + } catch (Throwable t) { + // Ignore me, use the default locale + } + return Locale.getDefault(); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLogger.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLogger.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLogger.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLogger.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,116 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + +import org.apache.log4j.Level; + + +/** + * Implementation of {@link Logger}, which is based on logj. + */ +public class Log4jLogger implements Logger { + private final org.apache.log4j.Logger log; + + /** + * Creates a new instance. + */ + public Log4jLogger(Class pClass) { + log = org.apache.log4j.Logger.getLogger(pClass); + } + + /** + * Creates a new instance. + */ + public Log4jLogger(String pId) { + log = org.apache.log4j.Logger.getLogger(pId); + } + + private String asString(Object[] pParams) { + final StringBuilder sb = new StringBuilder(); + if (pParams != null) { + for (int i = 0; i < pParams.length; i++) { + if (i > 0) { + sb.append(", "); //$NON-NLS-1$ + } + sb.append(pParams[i]); + } + } + return sb.toString(); + } + + @Override + public boolean isDebugEnabled() { + return log.isDebugEnabled(); + } + + @Override + public boolean isErrorEnabled() { + return log.isEnabledFor(Level.ERROR); + } + + @Override + public void entering(String pName) { + if (log.isDebugEnabled()) { + log.debug(pName + ": ->"); //$NON-NLS-1$ + } + } + + @Override + public void entering(String pName, String pMsg) { + if (log.isDebugEnabled()) { + log.debug(pName + ": -> " + pMsg); //$NON-NLS-1$ + } + } + + @Override + public void entering(String pName, String pMsg, Object... pArgs) { + if (log.isDebugEnabled()) { + log.debug(pName + ": -> " + pMsg + ", " + asString(pArgs)); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + @Override + public void exiting(String pName) { + if (log.isDebugEnabled()) { + log.debug(pName + ": <-"); //$NON-NLS-1$ + } + } + + @Override + public void exiting(String pName, String pMsg) { + if (log.isDebugEnabled()) { + log.debug(pName + ": <- " + pMsg); //$NON-NLS-1$ + } + } + + @Override + public void debug(String pName, String pMsg) { + if (log.isDebugEnabled()) { + log.debug(pName + ": " + pMsg); //$NON-NLS-1$ + } + } + + @Override + public void error(String pName, Throwable pThrowable) { + if (isErrorEnabled()) { + final String msg = pThrowable.getMessage() == null ? pThrowable.getClass().getName() : pThrowable.getMessage(); + log.error(pName + ": " + msg, pThrowable); //$NON-NLS-1$ + } + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLoggerFactory.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLoggerFactory.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLoggerFactory.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Log4jLoggerFactory.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + +import java.net.URL; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.PropertyConfigurator; + + +/** + * Default implementation of {@link LoggerFactory}. + */ +public class Log4jLoggerFactory implements LoggerFactory { + /** + * Creates a new instance with default settings. + */ + public Log4jLoggerFactory() { + final URL url = Thread.currentThread().getContextClassLoader().getResource("log4j.properties"); //$NON-NLS-1$ + if (url == null) { + if (!org.apache.log4j.Logger.getRootLogger().getAllAppenders().hasMoreElements()) { + BasicConfigurator.configure(); + } + } else { + if ("file".equals(url.getProtocol())) { //$NON-NLS-1$ + PropertyConfigurator.configureAndWatch(url.getFile()); + } else { + PropertyConfigurator.configure(url); + } + } + } + + @Override + public Logger newLogger(Class pClass) { + return new Log4jLogger(pClass); + } + + @Override + public Logger newLogger(String pId) { + return new Log4jLogger(pId); + } +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Logger.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Logger.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Logger.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Logger.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + + + +/** + * Interface of a logger. + */ +public interface Logger { + /** + * Returns, whether debug messages are enabled. + */ + boolean isDebugEnabled(); + + /** + * Returns, whether error messages are enabled. + */ + boolean isErrorEnabled(); + + /** + * Logs the given error. + */ + void error(String pName, Throwable pThrowable); + + /** + * Logs, that a method is being entered. + */ + void entering(String mName); + + /** + * Logs, that a method is being entered. + */ + void entering(String mName, String pMsg); + + /** + * Logs, that a method is being entered. + */ + void entering(String pName, String pMsg, Object... pArgs); + + /** + * Logs, that a method is being exited. + */ + void exiting(String mName); + + /** + * Logs, that a method is being exited. + */ + void exiting(String mName, String pMsg); + + /** + * Logs a debugging message. + * @param pName The method name. + * @param pMsg The message being logged. + */ + void debug(String pName, String pMsg); +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/LoggerFactory.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/LoggerFactory.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/LoggerFactory.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/LoggerFactory.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + + +/** + * Interface of a factory for loggers. + */ +public interface LoggerFactory { + /** + * Creates a new {@link Logger}. Shortcut for + *
newLogger(pClass.getName())
. + */ + Logger newLogger(Class pClass); + + /** + * Creates a new {@link Logger}. + */ + Logger newLogger(String pId); +} Added: labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Strings.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Strings.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Strings.java (added) +++ labs/jaxmas/trunk/JaxMas/src/main/java/org/apache/labs/jaxmas/registry/util/Strings.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry.util; + + +/** + * Utility class for working with strings. + */ +public class Strings { + /** + * Private constructor, to prevent accidental instantiation + */ + private Strings() { + // Does nothing + } + + /** + * Returns, whether the given string is null or empty. + */ + public static boolean isEmpty(String pValue) { + return pValue == null || pValue.length() == 0; + } + + /** + * Returns, whether the given string is null or empty after trimming. + */ + public static boolean isTrimmedEmpty(String pValue) { + return pValue == null || pValue.trim().length() == 0; + } +} Added: labs/jaxmas/trunk/JaxMas/src/test/java/org/apache/labs/jaxmas/registry/AbstractInitializedJaxMasTestCase.java URL: http://svn.apache.org/viewvc/labs/jaxmas/trunk/JaxMas/src/test/java/org/apache/labs/jaxmas/registry/AbstractInitializedJaxMasTestCase.java?rev=680187&view=auto ============================================================================== --- labs/jaxmas/trunk/JaxMas/src/test/java/org/apache/labs/jaxmas/registry/AbstractInitializedJaxMasTestCase.java (added) +++ labs/jaxmas/trunk/JaxMas/src/test/java/org/apache/labs/jaxmas/registry/AbstractInitializedJaxMasTestCase.java Sun Jul 27 14:13:00 2008 @@ -0,0 +1,150 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.labs.jaxmas.registry; + +import java.util.StringTokenizer; + +import javax.xml.registry.BusinessLifeCycleManager; +import javax.xml.registry.BusinessQueryManager; +import javax.xml.registry.JAXRException; +import javax.xml.registry.infomodel.ClassificationScheme; +import javax.xml.registry.infomodel.Concept; + +import org.apache.labs.jaxmas.registry.infomodel.RegistryServiceImpl; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; + + +/** + * Abstract base class for test cases, where an initialized database + * and an open registry connection are expected. + */ +public abstract class AbstractInitializedJaxMasTestCase extends AbstractJaxMasTestCase { + /** + * Called to ensure that the database is initialized. + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + initDatabase(); + } + + /** + * Called to open a database connection. + */ + @Before @Override + public void setUp() throws Exception { + initConnection(); + } + + /** + * Called to close the database connection, which has been + * opened by {@link #setUp()}. + */ + @After @Override + public void tearDown() throws Exception { + closeConnection(); + } + + /** + * Called after the test is finished in order to shutdown the database. + */ + @AfterClass + public void tearDownAfterClass() throws Exception { + initConnection(); + RegistryServiceImpl.getDbDriver(getConnection().getRegistryService()).shutdown(); + } + + /** + * Returns the current connections business query manager. + */ + protected BusinessQueryManager getBusinessQueryManager() throws JAXRException { + return getConnection().getRegistryService().getBusinessQueryManager(); + } + + /** + * Returns the current connections business lifecycle manager. + */ + protected BusinessLifeCycleManager getBusinessLifecycleManager() throws JAXRException { + return getConnection().getRegistryService().getBusinessLifeCycleManager(); + } + + /** + * Returns the classification scheme with the given name. + */ + protected ClassificationScheme findClassificationScheme(String pName) throws JAXRException { + return getBusinessQueryManager().findClassificationSchemeByName(null, pName); + } + + /** + * Returns the concept with the given value, the given classification scheme, and no parent concept. + */ + protected Concept findConcept(ClassificationScheme pClassificationScheme, String pValue) throws JAXRException { + for (Object o : pClassificationScheme.getChildrenConcepts()) { + final Concept concept = (Concept) o; + if (pValue.equals(concept.getValue())) { + return concept; + } + } + return null; + } + + /** + * Returns the concept with the given value, and the given parent concept. + */ + protected Concept findConcept(Concept pParent, String pValue) throws JAXRException { + for (Object o : pParent.getChildrenConcepts()) { + final Concept concept = (Concept) o; + if (pValue.equals(concept.getValue())) { + return concept; + } + } + return null; + } + + /** + * Searches a concept by its path. + */ + public Concept getConceptByPath(String pPath) throws JAXRException { + final StringTokenizer st = new StringTokenizer(pPath, "/"); //$NON-NLS-1$ + if (!st.hasMoreTokens()) { + fail("Invalid concept path: " + pPath); //$NON-NLS-1$ + } + final String classificationSchemeName = st.nextToken(); + if (!st.hasMoreTokens()) { + fail("Invalid concept path: " + pPath); //$NON-NLS-1$ + } + final ClassificationScheme cs = findClassificationScheme(classificationSchemeName); + if (cs == null) { + return null; + } + Concept concept = findConcept(cs, st.nextToken()); + if (concept == null) { + return null; + } + while (st.hasMoreTokens()) { + concept = findConcept(concept, st.nextToken()); + if (concept == null) { + return null; + } + } + return concept; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org For additional commands, e-mail: commits-help@labs.apache.org