You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by at...@apache.org on 2008/12/12 13:07:04 UTC
svn commit: r725977 [25/48] - in /portals/jetspeed-2/portal/trunk: ./
app-servers/security/jboss/src/java/META-INF/jboss-secsvc/
app-servers/security/jboss/src/java/org/apache/jetspeed/appservers/security/jboss/
applications/jetspeed/src/main/javascrip...
Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/SecurityPolicies.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/SecurityPolicies.java?rev=725977&r1=725976&r2=725977&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/SecurityPolicies.java (original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/SecurityPolicies.java Fri Dec 12 04:06:29 2008
@@ -14,123 +14,123 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.jetspeed.security;
-
-import java.security.Policy;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * <p>
- * This class is used to hold the security that will be used when applying security policies. It
- * uses a singleton pattern to maintain state of the policies configured in the consuming engine.
- * </p>
- *
- * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
- */
-public class SecurityPolicies
-{
- /** The singleton instance. */
- private static SecurityPolicies instance = null;
-
- /** The list of wrapped policies. */
- private List wrappedPolicies = new ArrayList();
-
- /** The list of policies. */
- private List policies = new ArrayList();
-
- /** The list of used policies. */
- private List usedPolicies = new ArrayList();
-
- /**
- * Default contructor. Private to force singleton.
- */
- private SecurityPolicies()
- {
- }
-
- /**
- * <p>
- * Returns the singleton instance for SecurityPolicies.
- * </p>
- *
- * @return The instance of SecurityPolicies
- */
- public static SecurityPolicies getInstance()
- {
- if (instance == null)
- {
- instance = new SecurityPolicies();
- }
- return instance;
- }
-
- /**
- * <p>
- * Adds a policy to the list of policies to enforces.
- * </p>
- *
- * @param wrappedPolicy The {@link PolicyWrapper} to add.
- */
- public void addPolicy(PolicyWrapper wrappedPolicy)
- {
- if (null != wrappedPolicy)
- {
- wrappedPolicies.add(wrappedPolicy);
- policies.add(wrappedPolicy.getPolicy());
- if (wrappedPolicy.isUseAsPolicy())
- {
- usedPolicies.add(wrappedPolicy.getPolicy());
- }
- }
-
- }
-
- /**
- * <p>
- * Returns the security policies to enforce as list of {@link Policy}.
- * </p>
- *
- * @return The policies.
- */
- public List getPolicies()
- {
- return policies;
- }
-
- /**
- * <p>
- * Returns the security policies to be enforced as list of {@link Policy}.
- * </p>
- *
- * @return The used policies.
- */
- public List getUsedPolicies()
- {
- return usedPolicies;
- }
-
- /**
- * <p>
- * Returns the security policies to enforce as list of {@link PolicyWrapper}.
- * </p>
- *
- * @return The policies.
- */
- public List getWrappedPolicies()
- {
- return wrappedPolicies;
- }
-
- /**
- * <p>
- * Removes a policy from the list of policies to enforces.
- * </p>
- *
- * @param policy The {@link Policy} to add.
- */
- public void removePolicy(PolicyWrapper policy)
- {
- wrappedPolicies.remove(policy);
- }
-}
+package org.apache.jetspeed.security;
+
+import java.security.Policy;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <p>
+ * This class is used to hold the security that will be used when applying security policies. It
+ * uses a singleton pattern to maintain state of the policies configured in the consuming engine.
+ * </p>
+ *
+ * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
+ */
+public class SecurityPolicies
+{
+ /** The singleton instance. */
+ private static SecurityPolicies instance = null;
+
+ /** The list of wrapped policies. */
+ private List wrappedPolicies = new ArrayList();
+
+ /** The list of policies. */
+ private List policies = new ArrayList();
+
+ /** The list of used policies. */
+ private List usedPolicies = new ArrayList();
+
+ /**
+ * Default contructor. Private to force singleton.
+ */
+ private SecurityPolicies()
+ {
+ }
+
+ /**
+ * <p>
+ * Returns the singleton instance for SecurityPolicies.
+ * </p>
+ *
+ * @return The instance of SecurityPolicies
+ */
+ public static SecurityPolicies getInstance()
+ {
+ if (instance == null)
+ {
+ instance = new SecurityPolicies();
+ }
+ return instance;
+ }
+
+ /**
+ * <p>
+ * Adds a policy to the list of policies to enforces.
+ * </p>
+ *
+ * @param wrappedPolicy The {@link PolicyWrapper} to add.
+ */
+ public void addPolicy(PolicyWrapper wrappedPolicy)
+ {
+ if (null != wrappedPolicy)
+ {
+ wrappedPolicies.add(wrappedPolicy);
+ policies.add(wrappedPolicy.getPolicy());
+ if (wrappedPolicy.isUseAsPolicy())
+ {
+ usedPolicies.add(wrappedPolicy.getPolicy());
+ }
+ }
+
+ }
+
+ /**
+ * <p>
+ * Returns the security policies to enforce as list of {@link Policy}.
+ * </p>
+ *
+ * @return The policies.
+ */
+ public List getPolicies()
+ {
+ return policies;
+ }
+
+ /**
+ * <p>
+ * Returns the security policies to be enforced as list of {@link Policy}.
+ * </p>
+ *
+ * @return The used policies.
+ */
+ public List getUsedPolicies()
+ {
+ return usedPolicies;
+ }
+
+ /**
+ * <p>
+ * Returns the security policies to enforce as list of {@link PolicyWrapper}.
+ * </p>
+ *
+ * @return The policies.
+ */
+ public List getWrappedPolicies()
+ {
+ return wrappedPolicies;
+ }
+
+ /**
+ * <p>
+ * Removes a policy from the list of policies to enforces.
+ * </p>
+ *
+ * @param policy The {@link Policy} to add.
+ */
+ public void removePolicy(PolicyWrapper policy)
+ {
+ wrappedPolicies.remove(policy);
+ }
+}
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/SecurityPolicies.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/activeauthentication/ActiveAuthenticationIdentityProviderImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/activeauthentication/IdentityTokenImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/impl/JaasPolicyCoordinator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/AlgorithmUpgradeCredentialPasswordEncoder.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/AlgorithmUpgradeCredentialPasswordEncoder.java?rev=725977&r1=725976&r2=725977&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/AlgorithmUpgradeCredentialPasswordEncoder.java (original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/AlgorithmUpgradeCredentialPasswordEncoder.java Fri Dec 12 04:06:29 2008
@@ -4,41 +4,41 @@
* 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.jetspeed.security.spi;
-
+ * 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.jetspeed.security.spi;
+
import org.apache.jetspeed.security.CredentialPasswordEncoder;
-import org.apache.jetspeed.security.PasswordCredential;
-import org.apache.jetspeed.security.SecurityException;
-
-/**
- * <p>
- * AlgorithmUpgradeCredentialPasswordEncoder which is provided with the PasswordCredential as well
- * to allow for migrating between two different encoding schemes.
- * </p>
- * <p>
- * The extended encode method is *only* called in the context of validating an existing (old) password,
- * and not used for creating or updating to a new password directl!
- * </p>
- * <p>
- * After successfull authentication, the recodeIfNeeded method will be called allowing to migrate to the new encryption scheme.
- * </p>
- *
- * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
- * @version $Id$
- */
-public interface AlgorithmUpgradeCredentialPasswordEncoder extends CredentialPasswordEncoder
-{
- String encode(PasswordCredential credential, String clearTextPassword) throws SecurityException;
- void recodeIfNeeded(PasswordCredential credential, String clearTextPassword) throws SecurityException;
- boolean usesOldEncodingAlgorithm(PasswordCredential credential);
-}
+import org.apache.jetspeed.security.PasswordCredential;
+import org.apache.jetspeed.security.SecurityException;
+
+/**
+ * <p>
+ * AlgorithmUpgradeCredentialPasswordEncoder which is provided with the PasswordCredential as well
+ * to allow for migrating between two different encoding schemes.
+ * </p>
+ * <p>
+ * The extended encode method is *only* called in the context of validating an existing (old) password,
+ * and not used for creating or updating to a new password directl!
+ * </p>
+ * <p>
+ * After successfull authentication, the recodeIfNeeded method will be called allowing to migrate to the new encryption scheme.
+ * </p>
+ *
+ * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
+ * @version $Id$
+ */
+public interface AlgorithmUpgradeCredentialPasswordEncoder extends CredentialPasswordEncoder
+{
+ String encode(PasswordCredential credential, String clearTextPassword) throws SecurityException;
+ void recodeIfNeeded(PasswordCredential credential, String clearTextPassword) throws SecurityException;
+ boolean usesOldEncodingAlgorithm(PasswordCredential credential);
+}
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/AlgorithmUpgradeCredentialPasswordEncoder.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/PasswordCredentialInterceptor.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/PasswordCredentialInterceptor.java?rev=725977&r1=725976&r2=725977&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/PasswordCredentialInterceptor.java (original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/PasswordCredentialInterceptor.java Fri Dec 12 04:06:29 2008
@@ -4,99 +4,99 @@
* 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.jetspeed.security.spi;
-
+ * 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.jetspeed.security.spi;
+
import org.apache.jetspeed.security.CredentialPasswordEncoder;
import org.apache.jetspeed.security.CredentialPasswordValidator;
import org.apache.jetspeed.security.PasswordCredential;
-import org.apache.jetspeed.security.SecurityException;
-
-/**
- * <p>
- * Callback component interface to be used by the {@link UserPasswordCredentialPasswordPolicyManager}
- * allowing injecting custom logic on certain events of the {@link PasswordCredential}.
- * </p>
- *
- * @author <a href="mailto:ate@apache.org">Ate Douma</a>
- * @version $Id: InternalPasswordCredentialInterceptor.java 291016 2005-09-22 21:19:36Z ate $
- */
-public interface PasswordCredentialInterceptor
-{
- /**
- * <p>
- * Invoked after a password credential is loaded from the persistent store.</p>
- * <p>
- * If true is returned the credential is expected to be updated and its changes will be stored again.</p>
- * <p>
- * A thrown SecurityException will be logged as an error and result in the credential to be ignored
- * as if not existing (like for authentication).</p>
- *
- * @param userName the name of the principal to which the credential belongs
- * @param credential the credential just loaded from the persistent store
+import org.apache.jetspeed.security.SecurityException;
+
+/**
+ * <p>
+ * Callback component interface to be used by the {@link UserPasswordCredentialPasswordPolicyManager}
+ * allowing injecting custom logic on certain events of the {@link PasswordCredential}.
+ * </p>
+ *
+ * @author <a href="mailto:ate@apache.org">Ate Douma</a>
+ * @version $Id: InternalPasswordCredentialInterceptor.java 291016 2005-09-22 21:19:36Z ate $
+ */
+public interface PasswordCredentialInterceptor
+{
+ /**
+ * <p>
+ * Invoked after a password credential is loaded from the persistent store.</p>
+ * <p>
+ * If true is returned the credential is expected to be updated and its changes will be stored again.</p>
+ * <p>
+ * A thrown SecurityException will be logged as an error and result in the credential to be ignored
+ * as if not existing (like for authentication).</p>
+ *
+ * @param userName the name of the principal to which the credential belongs
+ * @param credential the credential just loaded from the persistent store
* @param encoder
* @param validator
- * @return true if the credential is updated
- * @throws SecurityException
- */
- boolean afterLoad(String userName, PasswordCredential credential, CredentialPasswordEncoder encoder, CredentialPasswordValidator validator) throws SecurityException;
-
- /**
- * <p>
- * Invoked during authentication after the provided password is compared against the one retrieved from
- * the PasswordCredential.</p>
- * <p>
- * If true is returned, the credential is expected to be updated and its {@link PasswordCredential#isEnabled() enabled}
- * and {@link PasswordCredential#isExpired() expired} flags will checked if the credential is (still) valid.</p>
- * <p>
- * Note: the enabled and expired flags are <em>only</em> checked if this method returns true.</p>
- * <p>
- * A thrown SecurityException will be passed on to the authentication requestor.</p>
- *
- * @param credential the credential of the user
- * @param authenticated true if the provided password matches the value of the credential
- * @return true if the credential is updated
- * @throws SecurityException
- */
- boolean afterAuthenticated(PasswordCredential credential, boolean authenticated) throws SecurityException;
-
- /**
- * <p>
- * Invoked when the first password credential is to be saved for a user.</p>
- * <p>
- * This callback method can be used to set default values like the {@link PasswordCredential#getExpirationDate() expiration date}.</p>
- * <p>
- * A thrown SecurityException is passed on to the new password requestor.</p>
- *
- * @param credential the credential of the user
- * @throws SecurityException
- */
- void beforeCreate(PasswordCredential credential) throws SecurityException;
-
- /**
- * <p>
- * Invoked when a new password value is to be set for a user.</p>
- * <p>
+ * @return true if the credential is updated
+ * @throws SecurityException
+ */
+ boolean afterLoad(String userName, PasswordCredential credential, CredentialPasswordEncoder encoder, CredentialPasswordValidator validator) throws SecurityException;
+
+ /**
+ * <p>
+ * Invoked during authentication after the provided password is compared against the one retrieved from
+ * the PasswordCredential.</p>
+ * <p>
+ * If true is returned, the credential is expected to be updated and its {@link PasswordCredential#isEnabled() enabled}
+ * and {@link PasswordCredential#isExpired() expired} flags will checked if the credential is (still) valid.</p>
+ * <p>
+ * Note: the enabled and expired flags are <em>only</em> checked if this method returns true.</p>
+ * <p>
+ * A thrown SecurityException will be passed on to the authentication requestor.</p>
+ *
+ * @param credential the credential of the user
+ * @param authenticated true if the provided password matches the value of the credential
+ * @return true if the credential is updated
+ * @throws SecurityException
+ */
+ boolean afterAuthenticated(PasswordCredential credential, boolean authenticated) throws SecurityException;
+
+ /**
+ * <p>
+ * Invoked when the first password credential is to be saved for a user.</p>
+ * <p>
+ * This callback method can be used to set default values like the {@link PasswordCredential#getExpirationDate() expiration date}.</p>
+ * <p>
+ * A thrown SecurityException is passed on to the new password requestor.</p>
+ *
+ * @param credential the credential of the user
+ * @throws SecurityException
+ */
+ void beforeCreate(PasswordCredential credential) throws SecurityException;
+
+ /**
+ * <p>
+ * Invoked when a new password value is to be set for a user.</p>
+ * <p>
* The new raw, possibly encoded, password value is <em>not</em> yet set on the provided credential when this callback is invoked but provided as parameter.
- * This allows custom history maintenance and/or auditing to be performed.</p>
- * After this callback is invoked, the password raw value will be set, as well as a reset of the
- * {@link PasswordCredential#isUpdateRequired() updateRequired} flag, before the credential is saved.</p>
- * <p>
- * A thrown SecurityException is passed on to the set password requestor.</p>
- *
+ * This allows custom history maintenance and/or auditing to be performed.</p>
+ * After this callback is invoked, the password raw value will be set, as well as a reset of the
+ * {@link PasswordCredential#isUpdateRequired() updateRequired} flag, before the credential is saved.</p>
+ * <p>
+ * A thrown SecurityException is passed on to the set password requestor.</p>
+ *
* @param credential the credential of the user
* @param password
- * @param authenticated
- * @throws SecurityException
- */
- void beforeSetPassword(PasswordCredential credential, String password, boolean authenticated) throws SecurityException;
-}
+ * @param authenticated
+ * @throws SecurityException
+ */
+ void beforeSetPassword(PasswordCredential credential, String password, boolean authenticated) throws SecurityException;
+}
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/PasswordCredentialInterceptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/AlgorithmUpgradePBEPasswordService.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/FragmentPermission.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PBEPasswordService.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/util/PBEPasswordTool.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/apacheds/setup1/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/apacheds/setup2/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/apacheds/setup3/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/apacheds/setup4/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/apacheds/setup5/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/domino/setup1/company.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/domino/setup2/company.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/domino/setup3/company.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/openldap/setup1/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/openldap/setup2/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/openldap/setup3/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/openldap/setup4/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/JETSPEED-INF/directory/config/sunds/setup1/company1.ldif
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-security/src/test/java/org/apache/jetspeed/security/util/TestPBEPasswordTool.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-serializer/src/main/java/org/apache/jetspeed/serializer/JetspeedDDLApplication.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-serializer/src/main/java/org/apache/jetspeed/serializer/JetspeedDDLApplication.java?rev=725977&r1=725976&r2=725977&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-serializer/src/main/java/org/apache/jetspeed/serializer/JetspeedDDLApplication.java (original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-serializer/src/main/java/org/apache/jetspeed/serializer/JetspeedDDLApplication.java Fri Dec 12 04:06:29 2008
@@ -1,827 +1,827 @@
-/*
- * 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.jetspeed.serializer;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileWriter;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import javolution.xml.XMLBinding;
-import javolution.xml.XMLObjectReader;
-
-import org.apache.commons.configuration.PropertiesConfiguration;
-import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.model.Table;
-import org.apache.jetspeed.serializer.objects.JSGroup;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
-/**
- * Jetspeed Serializer DDL- Application
- *
- * invoke with mandatory either
- * <p>
- * -I directory or filename of schema files for input, if directory all xml
- * files will be processed
- * </p>
- * and/or
- * <p>
- * -O schema file for output log of current database
- * </p>
- * and (if -I denotes a directory:)
- * <p>
- * -x if directory list provided for input schemas this pattern is excluded
- * </p>
- * <p>
- * -s name of the order file (if schema directory)
- * </p>
- *
- * <p>
- * -m if directory list provided this is the merge file to use. If not set here
- * or in the properties file, a hardcoded version is used
- * </p>
- *
- * <p>
- * note that - if -I and -O are specified, the output file will contain the
- * UPDATED database
- * </p>
- * invoke with (optional) parameters
- * <p>
- * -P propertyFileName, for settings
- * <p>
- * -R (flag) replace : overwrites default "UPDATE" and clears out the database
- * before processing (ignored with -O option above)
- * </p>
- *
- * <p>
- * -dn databaseName, for example MYSQL or ORACLE10
- * </p>
- * <p>
- * -dc driverClass, for example com.mysql.jdbc.Driver
- * </p>
- * <p>
- * -ds url, ruls according to the driver used, URL needs to point to the correct
- * database
- * </p>
- * <p>
- * -du user, user with create/drop etc. rights on the database
- * </p>
- * <p>
- * -dp password
- * </p>
- * <p>
- * -l log4j-level, ERROR (default), WARN, INFO
- * </p>
- *
- * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
- * @version $Id: $
- */
-public class JetspeedDDLApplication
-{
- public static final String JNDI_DS_NAME = "jetspeed";
-
- String propertyFileName = null;
- String exludeFileName = null;
- String orderFileName = null;
-
- String logLevel = null;
-
- PropertiesConfiguration configuration = null;
-
- boolean doImport = false;
- boolean doExport = false;
- String schemaDirectory = null; // if specified all xml files in that
- // directory will be processed
- String outputFile = null; // if specified the database schema will be
- // exported to that file
- boolean overwrite = false; // default, do not overwrite the database
- // (ignored if only output)
- String driverClass = null; // jdbc driver
- String url = null; // jdbc url to database
- String user = null; // user
- String password = null; // password
- String databaseName = null;
-
- String[] filesToProcess = null;
-
- String mergeFile = null; //name of XSLT merge file
- String[] args = null;
-
- public static void main(String[] args) throws Exception
- {
- JetspeedDDLApplication app = new JetspeedDDLApplication();
- app.processArguments(args);
- }
-
- public JetspeedDDLApplication()
- {
- }
-
- /**
- * ensure that we have valid database settings
- *
- */
- private void checkDBSettings()
- {
- if (databaseName == null)
- databaseName = System.getProperty(
- "org.apache.jetspeed.database.databaseName",
- "mysql");
- if (driverClass == null)
- driverClass = System.getProperty(
- "org.apache.jetspeed.database.driverClass",
- "com.mysql.jdbc.Driver");
- if (url == null)
- url = System.getProperty("org.apache.jetspeed.database.url",
- "jdbc:mysql://localhost/j2test");
- if (user == null)
- user = System.getProperty("org.apache.jetspeed.database.user",
- "user");
- if (password == null)
- password = System.getProperty(
- "org.apache.jetspeed.database.password", "password");
-
- if (driverClass == null)
- throw new IllegalArgumentException(
- "Can't proceed without a valid driver");
- if (url == null)
- throw new IllegalArgumentException(
- "Can't proceed without a valid url to the target database");
- if (user == null)
- throw new IllegalArgumentException(
- "Can't proceed without a valid database user");
- return;
- }
-
- /**
- * parse arguments for process instructions, order and exclude files as well
- * as optional database arguments
- *
- */
- private void parseArguments()
- {
- // Parse all the command-Oine arguments
- for (int n = 0; n < args.length; n++)
- {
- if (args[n].equals("-I"))
- {
- doImport = true;
- schemaDirectory = args[++n];
- } else if (args[n].equals("-O"))
- {
- doExport = true;
- outputFile = args[++n];
- } else if (args[n].equals("-s"))
- {
- orderFileName = args[++n];
- }
- else if (args[n].equals("-x"))
- {
- exludeFileName = args[++n];
- }
- else if (args[n].equals("-m"))
- {
- mergeFile = args[++n];
- }
- else if (args[n].equals("-R"))
- overwrite = true;
- else if (args[n].equals("-dn"))
- {
- databaseName = args[++n];
- }
- else if (args[n].equals("-dc"))
- driverClass = args[++n];
- else if (args[n].equals("-ds"))
- url = args[++n];
- else if (args[n].equals("-du"))
- {
- if (((n + 1) >= args.length) || args[n + 1].startsWith("-d"))
- {
- user = "";
- } else
- {
- user = args[++n];
- }
- }
- else if (args[n].equals("-dp"))
- {
- if (((n + 1) >= args.length) || args[n + 1].startsWith("-d"))
- {
- password = "";
- } else
- {
- password = args[++n];
- }
- }
- else if (args[n].equals("-P"))
- propertyFileName = args[++n];
- else if (args[n].equals("-l"))
- logLevel = args[++n];
-
- else
- throw new IllegalArgumentException("Unknown argument: "
- + args[n]);
- }
-
- }
-
- /**
- * process provided filename or directory name
- *
- * @return one or more files to be processed
- */
- private String[] parseFiles()
- {
- String[] fileList = null;
- try
- {
- File dir = new File(schemaDirectory);
- if (!(dir.exists()))
- return fileList;
- if (!(dir.isDirectory()))
- {
- fileList = new String[1];
- fileList[0] = schemaDirectory;
- return fileList;
- }
- // Handling a directory
- LocalFilenameFilter filter = new LocalFilenameFilter(
- exludeFileName, orderFileName);
- File[] files = dir.listFiles(filter);
- if (files == null)
- return fileList;
-
- fileList = new String[files.length];
- String sortorderFile = filter.getSortFile();
- if (sortorderFile == null)
- {
- for (int i = 0; i < files.length; i++)
- fileList[i] = files[i].getAbsolutePath();
- return fileList;
- }
- try
- {
- ArrayList list = readOrderFile(sortorderFile);
- fileList = new String[files.length];
- if ((list == null) || (list.size() == 0))
- {
- for (int i = 0; i < files.length; i++)
- fileList[i] = files[i].getAbsolutePath();
- return fileList;
- }
- String[] tempList = new String[files.length];
- for (int i = 0; i < files.length; i++)
- tempList[i] = files[i].getName();
- Iterator _it = list.iterator();
- int j = 0;
- while (_it.hasNext())
- {
- String filename = null;
- try
- {
- filename = ((JSGroup) _it.next()).getName();
- } catch (Exception eeee)
- {
- }
- if (filename != null)
- {
- for (int i = 0; i < files.length; i++)
- {
- if (filename.equalsIgnoreCase(tempList[i]))
- {
- fileList[j++] = files[i].getAbsolutePath();
- tempList[i] = null;
- }
- }
- }
- }
- for (int i = 0; i < files.length; i++)
- {
- if (tempList[i] != null)
- fileList[j++] = files[i].getAbsolutePath();
- }
- return fileList;
- } catch (Exception eee)
- {
- eee.printStackTrace();
- return null;
- }
-
- } catch (Exception e)
- {
- e.printStackTrace();
- throw new IllegalArgumentException(
- "Processing the schema-directory " + schemaDirectory
- + " caused exception " + e.getLocalizedMessage());
- }
-
- }
-
- /**
- * setup environment by processing all arguments and call requested process
- * routine
- *
- * @param arguments
- * @throws Exception
- */
- private void processArguments(String[] arguments) throws Exception
- {
- this.args = arguments;
- if (args == null)
- throw new IllegalArgumentException(
- "Either a schema directory, a schema file or an output filename have to be defined (-D followed by a driectory, -I or -O followed by the filename");
-
- parseArguments();
-
- processPropertyFile();
-
- checkDBSettings();
-
- /**
- * The only required argument is the filename for either export or
- * import
- */
- if ((!doImport) && (!doExport))
- throw new IllegalArgumentException(
- "Either a schema directory, a schema file or an output filename have to be defined (-I or -O followed by the directory-/filename");
-
- if (doImport)
- {
- filesToProcess = parseFiles();
- if (filesToProcess == null)
- return;
- }
-
- /** create the instruction map */
- JetspeedDDLUtil ddlUtil = null;
-
- HashMap context = new HashMap();
- context.put(JetspeedDDLUtil.DATASOURCE_DATABASENAME, databaseName);
- context.put(JetspeedDDLUtil.DATASOURCE_DRIVER, driverClass);
- context.put(JetspeedDDLUtil.DATASOURCE_URL, url);
- context.put(JetspeedDDLUtil.DATASOURCE_USERNAME, user);
- context.put(JetspeedDDLUtil.DATASOURCE_PASSWORD, password);
-
- Logger logger = Logger.getLogger("org.apache.ddlutils");
- Level level = logger.getLevel();
- if (logLevel == null)
- logger.setLevel(Level.ERROR);
- else if (logLevel.equalsIgnoreCase("INFO"))
- logger.setLevel(Level.INFO);
- else if (logLevel.equalsIgnoreCase("WARN"))
- logger.setLevel(Level.WARN);
- else
- logger.setLevel(Level.ERROR);
-
- try
- {
- ddlUtil = new JetspeedDDLUtil();
- ddlUtil.startUp();
- ddlUtil.init(context);
- } catch (Exception e)
- {
- System.err.println("Failed to initialize Utility!!!!!");
- e.printStackTrace();
- System.exit(-1);
- }
- try
- {
- if (doImport)
- processImport(ddlUtil);
- if (doExport)
- processExport(ddlUtil);
- } catch (Exception e)
- {
- System.err.println("Failed to process XML "
- + (doExport ? "export" : "import") + ":" + e);
- e.printStackTrace();
- } finally
- {
- try
- {
- logger.setLevel(level);
- if (ddlUtil != null)
- ddlUtil.tearDown();
- } catch (Exception e1)
- {
- System.out
- .println("starter framework teardown caused exception "
- + e1.getLocalizedMessage());
- e1.printStackTrace();
-
- }
- }
-
- }
-
- /**
- * create/alter database
- *
- * @param ddlUtil
- */
- private void processImport(JetspeedDDLUtil ddlUtil)
- {
- String file = null;
- if ((filesToProcess == null) || (filesToProcess.length == 0))
- return;
- if (filesToProcess.length > 1)
- file = mergeFiles(filesToProcess);
-
- System.out.println("Importing " + file);
- Database db = ddlUtil.createDatabaseSchemaFromXML(file);
- try
- {
- if (overwrite)
- ddlUtil.createDatabase(db); // overwrite existing
- // database
- else
- ddlUtil.alterDatabase(db);
- System.out.println("Importing " + file + " completed");
- } catch (Exception ePr)
- {
- ePr.printStackTrace();
- // continue with the process despite that one of the files was
- // bad...
- }
-
- }
-
- /**
- * Helper routine to create a temporary file
- *
- * @param suffix
- * @return
- */
- private File createTemp(String suffix)
- {
- try
- {
- // Create temp file.
- File temp = File.createTempFile("tmp", suffix);
-
- // Delete temp file when program exits.
- temp.deleteOnExit();
- return temp;
- } catch (IOException e)
- {
- System.out.println("Failed to create temproary file with "
- + e.getLocalizedMessage());
- e.printStackTrace();
- return null;
- }
-
- }
- /**
- * Open the merge file from disk
- *
- * @param fileName
- * @return
- */
- private File createXSLTFromFile(String fileName)
- {
- if (fileName == null)
- return null;
- try
- {
- File f = new File(fileName);
- if (f.exists())
- return f;
- return null;
- }
- catch (Exception e)
- {
- System.out.println("Failed to open merge template " + e.getLocalizedMessage());
- e.printStackTrace();
- return null;
- }
- }
- /**
- * If everything else fails, use a hardcoded XSLT here
- *
- * @return
- */
- private File createXSLTFromMemory()
- {
- StringBuffer buffer = new StringBuffer();
-
- buffer.append("<?xml version=\"1.0\"?>");
- buffer
- .append("<xslt:transform version=\"1.0\" xmlns:xslt=\"http://www.w3.org/1999/XSL/Transform\">");
- buffer
- .append("<!-- Simple template to merge two database schemas into one -->");
- buffer.append("<xslt:param name=\"fileTwo\" />");
- buffer.append("<xslt:template match=\"/\">");
-
- buffer.append("<xslt:message>");
- buffer
- .append("<xslt:text /> Merging input with '<xslt:value-of select=\"$fileTwo\"/>");
- buffer.append("<xslt:text>'</xslt:text>");
- buffer.append("</xslt:message>");
- buffer.append("<xslt:if test=\"string($fileTwo)=''\">");
- buffer.append("<xslt:message terminate=\"yes\">");
- buffer
- .append("<xslt:text>No input file specified (parameter 'fileTwo')</xslt:text>");
- buffer.append("</xslt:message>");
- buffer.append("</xslt:if>");
- buffer.append("<database name=\"generic\">");
- buffer.append("<xslt:apply-templates />");
- buffer.append("</database>");
- buffer.append("</xslt:template>");
- buffer.append("<xslt:template match=\"database\">");
- buffer.append("<xslt:apply-templates />");
- buffer.append("<xslt:apply-templates select=\"document($fileTwo)/database/table\"/>");
- buffer.append("</xslt:template>");
-
- buffer.append("<xslt:template match=\"@*|node()\">");
- buffer.append("<xslt:copy>");
- buffer.append("<xslt:apply-templates select=\"@*|node()\"/>");
- buffer.append("</xslt:copy>");
- buffer.append("</xslt:template>");
- buffer.append("</xslt:transform>");
-
- File xslt = createTemp(".xslt");
- try
- {
- // Write to temp file
-
- BufferedWriter out = new BufferedWriter(new FileWriter(xslt));
- out.write(buffer.toString());
- out.close();
- return xslt;
- } catch (Exception e)
- {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * process of merging two or more schema files into one schema file.
- *
- * @param fileList The filelist contains a (potentially) ordered list of schemas
- * @return The name of the created temporary schema file
- */
- private String mergeFiles(String[] fileList)
- {
- try
- {
- File xsltFile = createXSLTFromFile(mergeFile);
- if (xsltFile == null)
- xsltFile = createXSLTFromMemory();
- Source xslt = new StreamSource(xsltFile);
- Transformer transformer = TransformerFactory.newInstance()
- .newTransformer(xslt);
-
- String sourceName = fileList[0];
- File target = null;
- for (int i = 1; i < fileList.length; i++)
- {
- File soureFile = new File(sourceName);
- Source source = new StreamSource(soureFile);
- // JAXP reads data using the Source interface
- target = createTemp(".xml");
-
- Result targetResult = new StreamResult(target);
- File f = new File(fileList[i]);
- String other = "file:///" + f.getCanonicalPath(); // required on Win-platforms
- other = other.replace('\\', '/');
-
- transformer.setParameter("fileTwo", other);
- transformer.transform(source, targetResult);
- sourceName = target.getAbsolutePath();
- }
- return sourceName;
-
- } catch (Exception e)
- {
- e.printStackTrace();
- return null;
- }
-
- }
-
- /**
- * read database schema to file
- *
- */
- private void processExport(JetspeedDDLUtil ddlUtil)
- {
- // TODO: implement
- ddlUtil.writeDatabaseSchematoFile(this.outputFile);
-
- }
-
- /**
- * read the property file and read what has not yet been defined
- */
- private void processPropertyFile()
- {
- /** get system property definition */
- if (propertyFileName == null)
- propertyFileName = System.getProperty(
- "org.apache.jetspeed.xml.ddlUtil.configuration", null);
-
- if (propertyFileName == null)
- return;
- try
- {
- configuration = new PropertiesConfiguration(propertyFileName);
- } catch (Exception e)
- {
- e.printStackTrace();
- return;
- }
- if (configuration != null)
- {
- /** only read what was not defined on the command line */
-
- if (driverClass == null)
- driverClass = configuration.getString("driverClass");
- if (url == null)
- url = configuration.getString("url");
- if (user == null)
- user = configuration.getString("user");
- if (password == null)
- password = configuration.getString("password");
- if (mergeFile == null)
- mergeFile = configuration.getString("mergeFile");
- if (!(doImport))
- {
- schemaDirectory = configuration.getString("schema");
- if (schemaDirectory != null)
- doImport = true;
- }
- if (!(doExport))
- {
- outputFile = configuration.getString("outputFile");
- if (outputFile != null)
- doExport = true;
- }
- if (logLevel == null)
- logLevel = configuration.getString("loglevel");
-
- }
-
- }
-
- /*
- private static String[] getTokens(String _line)
- {
- if ((_line == null) || (_line.length() == 0))
- return null;
-
- StringTokenizer st = new StringTokenizer(_line, ",");
- ArrayList list = new ArrayList();
-
- while (st.hasMoreTokens())
- list.add(st.nextToken());
- String[] s = new String[list.size()];
- for (int i = 0; i < list.size(); i++)
- s[i] = (String) list.get(i);
- return s;
- }
- */
-
- public List getRows(JetspeedDDLUtil ddlUtil, String tableName)
- {
- Table table = ddlUtil.getModel().findTable(tableName,
- ddlUtil.getPlatform().isDelimitedIdentifierModeOn());
-
- return ddlUtil.getPlatform().fetch(ddlUtil.getModel(),
- getSelectQueryForAllString(ddlUtil, table), new Table[]
- {table});
- }
-
- public String getSelectQueryForAllString(JetspeedDDLUtil ddlUtil,
- Table table)
- {
-
- StringBuffer query = new StringBuffer();
-
- query.append("SELECT * FROM ");
- if (ddlUtil.getPlatform().isDelimitedIdentifierModeOn())
- {
- query.append(ddlUtil.getPlatform().getPlatformInfo()
- .getDelimiterToken());
- }
- query.append(table.getName());
- if (ddlUtil.getPlatform().isDelimitedIdentifierModeOn())
- {
- query.append(ddlUtil.getPlatform().getPlatformInfo()
- .getDelimiterToken());
- }
- System.out.println(query.toString());
- return query.toString();
- }
-
- /**
- * read an xml file describing the basic order of the files to be processed
- *
- * @param importFileName
- * @return
- * @throws SerializerException
- */
-
- private ArrayList readOrderFile(String importFileName)
- {
- XMLObjectReader reader = null;
-
- XMLBinding binding = new XMLBinding();
- binding.setAlias(ArrayList.class, "ProcessOrder");
- binding.setAlias(JSGroup.class, "File");
-
- ArrayList snap = null;
- try
- {
- reader = XMLObjectReader.newInstance(new FileInputStream(
- importFileName));
- } catch (Exception e)
- {
- e.printStackTrace();
- return null;
- }
- try
- {
- reader.setBinding(binding);
- snap = (ArrayList) reader.read("ProcessOrder", ArrayList.class);
-
- } catch (Exception e)
- {
- e.printStackTrace();
- } finally
- {
- /** ensure the reader is closed */
- try
- {
- reader.close();
- } catch (Exception e1)
- {
- /**
- * don't do anything with this exception - never let the bubble
- * out of the finally block
- */
- }
- }
- return snap;
- }
-
- class LocalFilenameFilter implements FilenameFilter
- {
-
- String exclude = null;
- String sortFile = null;
- String sort = null;
-
- String getSortFile()
- {
- return sortFile;
- }
- LocalFilenameFilter(String exclude, String sort)
- {
- this.exclude = exclude;
- this.sort = sort;
-
- }
- public boolean accept(File dir, String name)
- {
- if (exclude != null)
- if (name.equalsIgnoreCase(exclude))
- return false;
- if (sort != null)
- if (name.equalsIgnoreCase(sort))
- {
- sortFile = dir.getAbsolutePath() + "/" + sort;
- return false;
- }
-
- return name.endsWith(".xml");
- }
-
- }
-
-}
+/*
+ * 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.jetspeed.serializer;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import javolution.xml.XMLBinding;
+import javolution.xml.XMLObjectReader;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.ddlutils.model.Database;
+import org.apache.ddlutils.model.Table;
+import org.apache.jetspeed.serializer.objects.JSGroup;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+/**
+ * Jetspeed Serializer DDL- Application
+ *
+ * invoke with mandatory either
+ * <p>
+ * -I directory or filename of schema files for input, if directory all xml
+ * files will be processed
+ * </p>
+ * and/or
+ * <p>
+ * -O schema file for output log of current database
+ * </p>
+ * and (if -I denotes a directory:)
+ * <p>
+ * -x if directory list provided for input schemas this pattern is excluded
+ * </p>
+ * <p>
+ * -s name of the order file (if schema directory)
+ * </p>
+ *
+ * <p>
+ * -m if directory list provided this is the merge file to use. If not set here
+ * or in the properties file, a hardcoded version is used
+ * </p>
+ *
+ * <p>
+ * note that - if -I and -O are specified, the output file will contain the
+ * UPDATED database
+ * </p>
+ * invoke with (optional) parameters
+ * <p>
+ * -P propertyFileName, for settings
+ * <p>
+ * -R (flag) replace : overwrites default "UPDATE" and clears out the database
+ * before processing (ignored with -O option above)
+ * </p>
+ *
+ * <p>
+ * -dn databaseName, for example MYSQL or ORACLE10
+ * </p>
+ * <p>
+ * -dc driverClass, for example com.mysql.jdbc.Driver
+ * </p>
+ * <p>
+ * -ds url, ruls according to the driver used, URL needs to point to the correct
+ * database
+ * </p>
+ * <p>
+ * -du user, user with create/drop etc. rights on the database
+ * </p>
+ * <p>
+ * -dp password
+ * </p>
+ * <p>
+ * -l log4j-level, ERROR (default), WARN, INFO
+ * </p>
+ *
+ * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
+ * @version $Id: $
+ */
+public class JetspeedDDLApplication
+{
+ public static final String JNDI_DS_NAME = "jetspeed";
+
+ String propertyFileName = null;
+ String exludeFileName = null;
+ String orderFileName = null;
+
+ String logLevel = null;
+
+ PropertiesConfiguration configuration = null;
+
+ boolean doImport = false;
+ boolean doExport = false;
+ String schemaDirectory = null; // if specified all xml files in that
+ // directory will be processed
+ String outputFile = null; // if specified the database schema will be
+ // exported to that file
+ boolean overwrite = false; // default, do not overwrite the database
+ // (ignored if only output)
+ String driverClass = null; // jdbc driver
+ String url = null; // jdbc url to database
+ String user = null; // user
+ String password = null; // password
+ String databaseName = null;
+
+ String[] filesToProcess = null;
+
+ String mergeFile = null; //name of XSLT merge file
+ String[] args = null;
+
+ public static void main(String[] args) throws Exception
+ {
+ JetspeedDDLApplication app = new JetspeedDDLApplication();
+ app.processArguments(args);
+ }
+
+ public JetspeedDDLApplication()
+ {
+ }
+
+ /**
+ * ensure that we have valid database settings
+ *
+ */
+ private void checkDBSettings()
+ {
+ if (databaseName == null)
+ databaseName = System.getProperty(
+ "org.apache.jetspeed.database.databaseName",
+ "mysql");
+ if (driverClass == null)
+ driverClass = System.getProperty(
+ "org.apache.jetspeed.database.driverClass",
+ "com.mysql.jdbc.Driver");
+ if (url == null)
+ url = System.getProperty("org.apache.jetspeed.database.url",
+ "jdbc:mysql://localhost/j2test");
+ if (user == null)
+ user = System.getProperty("org.apache.jetspeed.database.user",
+ "user");
+ if (password == null)
+ password = System.getProperty(
+ "org.apache.jetspeed.database.password", "password");
+
+ if (driverClass == null)
+ throw new IllegalArgumentException(
+ "Can't proceed without a valid driver");
+ if (url == null)
+ throw new IllegalArgumentException(
+ "Can't proceed without a valid url to the target database");
+ if (user == null)
+ throw new IllegalArgumentException(
+ "Can't proceed without a valid database user");
+ return;
+ }
+
+ /**
+ * parse arguments for process instructions, order and exclude files as well
+ * as optional database arguments
+ *
+ */
+ private void parseArguments()
+ {
+ // Parse all the command-Oine arguments
+ for (int n = 0; n < args.length; n++)
+ {
+ if (args[n].equals("-I"))
+ {
+ doImport = true;
+ schemaDirectory = args[++n];
+ } else if (args[n].equals("-O"))
+ {
+ doExport = true;
+ outputFile = args[++n];
+ } else if (args[n].equals("-s"))
+ {
+ orderFileName = args[++n];
+ }
+ else if (args[n].equals("-x"))
+ {
+ exludeFileName = args[++n];
+ }
+ else if (args[n].equals("-m"))
+ {
+ mergeFile = args[++n];
+ }
+ else if (args[n].equals("-R"))
+ overwrite = true;
+ else if (args[n].equals("-dn"))
+ {
+ databaseName = args[++n];
+ }
+ else if (args[n].equals("-dc"))
+ driverClass = args[++n];
+ else if (args[n].equals("-ds"))
+ url = args[++n];
+ else if (args[n].equals("-du"))
+ {
+ if (((n + 1) >= args.length) || args[n + 1].startsWith("-d"))
+ {
+ user = "";
+ } else
+ {
+ user = args[++n];
+ }
+ }
+ else if (args[n].equals("-dp"))
+ {
+ if (((n + 1) >= args.length) || args[n + 1].startsWith("-d"))
+ {
+ password = "";
+ } else
+ {
+ password = args[++n];
+ }
+ }
+ else if (args[n].equals("-P"))
+ propertyFileName = args[++n];
+ else if (args[n].equals("-l"))
+ logLevel = args[++n];
+
+ else
+ throw new IllegalArgumentException("Unknown argument: "
+ + args[n]);
+ }
+
+ }
+
+ /**
+ * process provided filename or directory name
+ *
+ * @return one or more files to be processed
+ */
+ private String[] parseFiles()
+ {
+ String[] fileList = null;
+ try
+ {
+ File dir = new File(schemaDirectory);
+ if (!(dir.exists()))
+ return fileList;
+ if (!(dir.isDirectory()))
+ {
+ fileList = new String[1];
+ fileList[0] = schemaDirectory;
+ return fileList;
+ }
+ // Handling a directory
+ LocalFilenameFilter filter = new LocalFilenameFilter(
+ exludeFileName, orderFileName);
+ File[] files = dir.listFiles(filter);
+ if (files == null)
+ return fileList;
+
+ fileList = new String[files.length];
+ String sortorderFile = filter.getSortFile();
+ if (sortorderFile == null)
+ {
+ for (int i = 0; i < files.length; i++)
+ fileList[i] = files[i].getAbsolutePath();
+ return fileList;
+ }
+ try
+ {
+ ArrayList list = readOrderFile(sortorderFile);
+ fileList = new String[files.length];
+ if ((list == null) || (list.size() == 0))
+ {
+ for (int i = 0; i < files.length; i++)
+ fileList[i] = files[i].getAbsolutePath();
+ return fileList;
+ }
+ String[] tempList = new String[files.length];
+ for (int i = 0; i < files.length; i++)
+ tempList[i] = files[i].getName();
+ Iterator _it = list.iterator();
+ int j = 0;
+ while (_it.hasNext())
+ {
+ String filename = null;
+ try
+ {
+ filename = ((JSGroup) _it.next()).getName();
+ } catch (Exception eeee)
+ {
+ }
+ if (filename != null)
+ {
+ for (int i = 0; i < files.length; i++)
+ {
+ if (filename.equalsIgnoreCase(tempList[i]))
+ {
+ fileList[j++] = files[i].getAbsolutePath();
+ tempList[i] = null;
+ }
+ }
+ }
+ }
+ for (int i = 0; i < files.length; i++)
+ {
+ if (tempList[i] != null)
+ fileList[j++] = files[i].getAbsolutePath();
+ }
+ return fileList;
+ } catch (Exception eee)
+ {
+ eee.printStackTrace();
+ return null;
+ }
+
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ throw new IllegalArgumentException(
+ "Processing the schema-directory " + schemaDirectory
+ + " caused exception " + e.getLocalizedMessage());
+ }
+
+ }
+
+ /**
+ * setup environment by processing all arguments and call requested process
+ * routine
+ *
+ * @param arguments
+ * @throws Exception
+ */
+ private void processArguments(String[] arguments) throws Exception
+ {
+ this.args = arguments;
+ if (args == null)
+ throw new IllegalArgumentException(
+ "Either a schema directory, a schema file or an output filename have to be defined (-D followed by a driectory, -I or -O followed by the filename");
+
+ parseArguments();
+
+ processPropertyFile();
+
+ checkDBSettings();
+
+ /**
+ * The only required argument is the filename for either export or
+ * import
+ */
+ if ((!doImport) && (!doExport))
+ throw new IllegalArgumentException(
+ "Either a schema directory, a schema file or an output filename have to be defined (-I or -O followed by the directory-/filename");
+
+ if (doImport)
+ {
+ filesToProcess = parseFiles();
+ if (filesToProcess == null)
+ return;
+ }
+
+ /** create the instruction map */
+ JetspeedDDLUtil ddlUtil = null;
+
+ HashMap context = new HashMap();
+ context.put(JetspeedDDLUtil.DATASOURCE_DATABASENAME, databaseName);
+ context.put(JetspeedDDLUtil.DATASOURCE_DRIVER, driverClass);
+ context.put(JetspeedDDLUtil.DATASOURCE_URL, url);
+ context.put(JetspeedDDLUtil.DATASOURCE_USERNAME, user);
+ context.put(JetspeedDDLUtil.DATASOURCE_PASSWORD, password);
+
+ Logger logger = Logger.getLogger("org.apache.ddlutils");
+ Level level = logger.getLevel();
+ if (logLevel == null)
+ logger.setLevel(Level.ERROR);
+ else if (logLevel.equalsIgnoreCase("INFO"))
+ logger.setLevel(Level.INFO);
+ else if (logLevel.equalsIgnoreCase("WARN"))
+ logger.setLevel(Level.WARN);
+ else
+ logger.setLevel(Level.ERROR);
+
+ try
+ {
+ ddlUtil = new JetspeedDDLUtil();
+ ddlUtil.startUp();
+ ddlUtil.init(context);
+ } catch (Exception e)
+ {
+ System.err.println("Failed to initialize Utility!!!!!");
+ e.printStackTrace();
+ System.exit(-1);
+ }
+ try
+ {
+ if (doImport)
+ processImport(ddlUtil);
+ if (doExport)
+ processExport(ddlUtil);
+ } catch (Exception e)
+ {
+ System.err.println("Failed to process XML "
+ + (doExport ? "export" : "import") + ":" + e);
+ e.printStackTrace();
+ } finally
+ {
+ try
+ {
+ logger.setLevel(level);
+ if (ddlUtil != null)
+ ddlUtil.tearDown();
+ } catch (Exception e1)
+ {
+ System.out
+ .println("starter framework teardown caused exception "
+ + e1.getLocalizedMessage());
+ e1.printStackTrace();
+
+ }
+ }
+
+ }
+
+ /**
+ * create/alter database
+ *
+ * @param ddlUtil
+ */
+ private void processImport(JetspeedDDLUtil ddlUtil)
+ {
+ String file = null;
+ if ((filesToProcess == null) || (filesToProcess.length == 0))
+ return;
+ if (filesToProcess.length > 1)
+ file = mergeFiles(filesToProcess);
+
+ System.out.println("Importing " + file);
+ Database db = ddlUtil.createDatabaseSchemaFromXML(file);
+ try
+ {
+ if (overwrite)
+ ddlUtil.createDatabase(db); // overwrite existing
+ // database
+ else
+ ddlUtil.alterDatabase(db);
+ System.out.println("Importing " + file + " completed");
+ } catch (Exception ePr)
+ {
+ ePr.printStackTrace();
+ // continue with the process despite that one of the files was
+ // bad...
+ }
+
+ }
+
+ /**
+ * Helper routine to create a temporary file
+ *
+ * @param suffix
+ * @return
+ */
+ private File createTemp(String suffix)
+ {
+ try
+ {
+ // Create temp file.
+ File temp = File.createTempFile("tmp", suffix);
+
+ // Delete temp file when program exits.
+ temp.deleteOnExit();
+ return temp;
+ } catch (IOException e)
+ {
+ System.out.println("Failed to create temproary file with "
+ + e.getLocalizedMessage());
+ e.printStackTrace();
+ return null;
+ }
+
+ }
+ /**
+ * Open the merge file from disk
+ *
+ * @param fileName
+ * @return
+ */
+ private File createXSLTFromFile(String fileName)
+ {
+ if (fileName == null)
+ return null;
+ try
+ {
+ File f = new File(fileName);
+ if (f.exists())
+ return f;
+ return null;
+ }
+ catch (Exception e)
+ {
+ System.out.println("Failed to open merge template " + e.getLocalizedMessage());
+ e.printStackTrace();
+ return null;
+ }
+ }
+ /**
+ * If everything else fails, use a hardcoded XSLT here
+ *
+ * @return
+ */
+ private File createXSLTFromMemory()
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append("<?xml version=\"1.0\"?>");
+ buffer
+ .append("<xslt:transform version=\"1.0\" xmlns:xslt=\"http://www.w3.org/1999/XSL/Transform\">");
+ buffer
+ .append("<!-- Simple template to merge two database schemas into one -->");
+ buffer.append("<xslt:param name=\"fileTwo\" />");
+ buffer.append("<xslt:template match=\"/\">");
+
+ buffer.append("<xslt:message>");
+ buffer
+ .append("<xslt:text /> Merging input with '<xslt:value-of select=\"$fileTwo\"/>");
+ buffer.append("<xslt:text>'</xslt:text>");
+ buffer.append("</xslt:message>");
+ buffer.append("<xslt:if test=\"string($fileTwo)=''\">");
+ buffer.append("<xslt:message terminate=\"yes\">");
+ buffer
+ .append("<xslt:text>No input file specified (parameter 'fileTwo')</xslt:text>");
+ buffer.append("</xslt:message>");
+ buffer.append("</xslt:if>");
+ buffer.append("<database name=\"generic\">");
+ buffer.append("<xslt:apply-templates />");
+ buffer.append("</database>");
+ buffer.append("</xslt:template>");
+ buffer.append("<xslt:template match=\"database\">");
+ buffer.append("<xslt:apply-templates />");
+ buffer.append("<xslt:apply-templates select=\"document($fileTwo)/database/table\"/>");
+ buffer.append("</xslt:template>");
+
+ buffer.append("<xslt:template match=\"@*|node()\">");
+ buffer.append("<xslt:copy>");
+ buffer.append("<xslt:apply-templates select=\"@*|node()\"/>");
+ buffer.append("</xslt:copy>");
+ buffer.append("</xslt:template>");
+ buffer.append("</xslt:transform>");
+
+ File xslt = createTemp(".xslt");
+ try
+ {
+ // Write to temp file
+
+ BufferedWriter out = new BufferedWriter(new FileWriter(xslt));
+ out.write(buffer.toString());
+ out.close();
+ return xslt;
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+ }
+ /**
+ * process of merging two or more schema files into one schema file.
+ *
+ * @param fileList The filelist contains a (potentially) ordered list of schemas
+ * @return The name of the created temporary schema file
+ */
+ private String mergeFiles(String[] fileList)
+ {
+ try
+ {
+ File xsltFile = createXSLTFromFile(mergeFile);
+ if (xsltFile == null)
+ xsltFile = createXSLTFromMemory();
+ Source xslt = new StreamSource(xsltFile);
+ Transformer transformer = TransformerFactory.newInstance()
+ .newTransformer(xslt);
+
+ String sourceName = fileList[0];
+ File target = null;
+ for (int i = 1; i < fileList.length; i++)
+ {
+ File soureFile = new File(sourceName);
+ Source source = new StreamSource(soureFile);
+ // JAXP reads data using the Source interface
+ target = createTemp(".xml");
+
+ Result targetResult = new StreamResult(target);
+ File f = new File(fileList[i]);
+ String other = "file:///" + f.getCanonicalPath(); // required on Win-platforms
+ other = other.replace('\\', '/');
+
+ transformer.setParameter("fileTwo", other);
+ transformer.transform(source, targetResult);
+ sourceName = target.getAbsolutePath();
+ }
+ return sourceName;
+
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+
+ }
+
+ /**
+ * read database schema to file
+ *
+ */
+ private void processExport(JetspeedDDLUtil ddlUtil)
+ {
+ // TODO: implement
+ ddlUtil.writeDatabaseSchematoFile(this.outputFile);
+
+ }
+
+ /**
+ * read the property file and read what has not yet been defined
+ */
+ private void processPropertyFile()
+ {
+ /** get system property definition */
+ if (propertyFileName == null)
+ propertyFileName = System.getProperty(
+ "org.apache.jetspeed.xml.ddlUtil.configuration", null);
+
+ if (propertyFileName == null)
+ return;
+ try
+ {
+ configuration = new PropertiesConfiguration(propertyFileName);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ return;
+ }
+ if (configuration != null)
+ {
+ /** only read what was not defined on the command line */
+
+ if (driverClass == null)
+ driverClass = configuration.getString("driverClass");
+ if (url == null)
+ url = configuration.getString("url");
+ if (user == null)
+ user = configuration.getString("user");
+ if (password == null)
+ password = configuration.getString("password");
+ if (mergeFile == null)
+ mergeFile = configuration.getString("mergeFile");
+ if (!(doImport))
+ {
+ schemaDirectory = configuration.getString("schema");
+ if (schemaDirectory != null)
+ doImport = true;
+ }
+ if (!(doExport))
+ {
+ outputFile = configuration.getString("outputFile");
+ if (outputFile != null)
+ doExport = true;
+ }
+ if (logLevel == null)
+ logLevel = configuration.getString("loglevel");
+
+ }
+
+ }
+
+ /*
+ private static String[] getTokens(String _line)
+ {
+ if ((_line == null) || (_line.length() == 0))
+ return null;
+
+ StringTokenizer st = new StringTokenizer(_line, ",");
+ ArrayList list = new ArrayList();
+
+ while (st.hasMoreTokens())
+ list.add(st.nextToken());
+ String[] s = new String[list.size()];
+ for (int i = 0; i < list.size(); i++)
+ s[i] = (String) list.get(i);
+ return s;
+ }
+ */
+
+ public List getRows(JetspeedDDLUtil ddlUtil, String tableName)
+ {
+ Table table = ddlUtil.getModel().findTable(tableName,
+ ddlUtil.getPlatform().isDelimitedIdentifierModeOn());
+
+ return ddlUtil.getPlatform().fetch(ddlUtil.getModel(),
+ getSelectQueryForAllString(ddlUtil, table), new Table[]
+ {table});
+ }
+
+ public String getSelectQueryForAllString(JetspeedDDLUtil ddlUtil,
+ Table table)
+ {
+
+ StringBuffer query = new StringBuffer();
+
+ query.append("SELECT * FROM ");
+ if (ddlUtil.getPlatform().isDelimitedIdentifierModeOn())
+ {
+ query.append(ddlUtil.getPlatform().getPlatformInfo()
+ .getDelimiterToken());
+ }
+ query.append(table.getName());
+ if (ddlUtil.getPlatform().isDelimitedIdentifierModeOn())
+ {
+ query.append(ddlUtil.getPlatform().getPlatformInfo()
+ .getDelimiterToken());
+ }
+ System.out.println(query.toString());
+ return query.toString();
+ }
+
+ /**
+ * read an xml file describing the basic order of the files to be processed
+ *
+ * @param importFileName
+ * @return
+ * @throws SerializerException
+ */
+
+ private ArrayList readOrderFile(String importFileName)
+ {
+ XMLObjectReader reader = null;
+
+ XMLBinding binding = new XMLBinding();
+ binding.setAlias(ArrayList.class, "ProcessOrder");
+ binding.setAlias(JSGroup.class, "File");
+
+ ArrayList snap = null;
+ try
+ {
+ reader = XMLObjectReader.newInstance(new FileInputStream(
+ importFileName));
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+ try
+ {
+ reader.setBinding(binding);
+ snap = (ArrayList) reader.read("ProcessOrder", ArrayList.class);
+
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ } finally
+ {
+ /** ensure the reader is closed */
+ try
+ {
+ reader.close();
+ } catch (Exception e1)
+ {
+ /**
+ * don't do anything with this exception - never let the bubble
+ * out of the finally block
+ */
+ }
+ }
+ return snap;
+ }
+
+ class LocalFilenameFilter implements FilenameFilter
+ {
+
+ String exclude = null;
+ String sortFile = null;
+ String sort = null;
+
+ String getSortFile()
+ {
+ return sortFile;
+ }
+ LocalFilenameFilter(String exclude, String sort)
+ {
+ this.exclude = exclude;
+ this.sort = sort;
+
+ }
+ public boolean accept(File dir, String name)
+ {
+ if (exclude != null)
+ if (name.equalsIgnoreCase(exclude))
+ return false;
+ if (sort != null)
+ if (name.equalsIgnoreCase(sort))
+ {
+ sortFile = dir.getAbsolutePath() + "/" + sort;
+ return false;
+ }
+
+ return name.endsWith(".xml");
+ }
+
+ }
+
+}
Propchange: portals/jetspeed-2/portal/trunk/components/jetspeed-serializer/src/main/java/org/apache/jetspeed/serializer/JetspeedDDLApplication.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org