001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.crypt;
018:
019: import java.lang.ref.WeakReference;
020:
021: import org.slf4j.Logger;
022: import org.slf4j.LoggerFactory;
023:
024: /**
025: *
026: * Crypt factory that creates the {@link ICrypt} object by instantiating a
027: * provided class. The class must implement {@link ICrypt}.
028: *
029: * @author Igor Vaynberg (ivaynberg)
030: */
031: public class ClassCryptFactory implements ICryptFactory {
032: private static final Logger log = LoggerFactory
033: .getLogger(ClassCryptFactory.class);
034: private final WeakReference/*<Class>*/cryptClass;
035: private final String encryptionKey;
036:
037: /**
038: * Construct.
039: *
040: * @param cryptClass
041: * class that will be instantiated to represent the ICrypt object
042: * @param encryptionKey
043: * encryption key
044: */
045: public ClassCryptFactory(Class cryptClass, String encryptionKey) {
046: if (cryptClass == null) {
047: throw new IllegalArgumentException(
048: "cryptClass cannot be null");
049: }
050:
051: if (!ICrypt.class.isAssignableFrom(cryptClass)) {
052: throw new IllegalArgumentException(
053: "cryptClass must implement ICrypt interface");
054: }
055:
056: this .cryptClass = new WeakReference(cryptClass);
057: this .encryptionKey = encryptionKey;
058: }
059:
060: /**
061: * @see org.apache.wicket.util.crypt.ICryptFactory#newCrypt()
062: */
063: public ICrypt newCrypt() {
064: try {
065: ICrypt crypt = (ICrypt) ((Class) cryptClass.get())
066: .newInstance();
067: log.info("using encryption/decryption object " + crypt);
068: crypt.setKey(encryptionKey);
069: return crypt;
070: } catch (Throwable e) {
071: log
072: .warn("************************** WARNING **************************");
073: log
074: .warn("As the instantion of encryption/decryption class:");
075: log.warn("\t" + cryptClass);
076: log
077: .warn("failed, Wicket will fallback on a dummy implementation");
078: log.warn("\t(" + NoCrypt.class.getName() + ")");
079: log.warn("This is not recommended for production systems.");
080: log
081: .warn("Please override method org.apache.wicket.Application.newCrypt()");
082: log
083: .warn("to provide a custom encryption/decryption implementation");
084: log.warn("The cause of the instantion failure: ");
085: log.warn("\t" + e.getMessage());
086: if (log.isDebugEnabled()) {
087: log.debug("exception: ", e);
088: } else {
089: log
090: .warn("set log level to DEBUG to display the stack trace.");
091: }
092: log
093: .warn("*************************************************************");
094:
095: // assign the dummy crypt implementation
096: return new NoCrypt();
097: }
098: }
099:
100: }
|