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 */ 017package org.apache.wicket.util.crypt; 018 019import java.lang.ref.WeakReference; 020 021import org.apache.wicket.util.lang.Args; 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024 025/** 026 * 027 * Crypt factory that creates the {@link ICrypt} object by instantiating a provided class. The class 028 * must implement {@link ICrypt}. 029 * 030 * @author Igor Vaynberg (ivaynberg) 031 * 032 * @deprecated use a lambda expression instead TODO remove in Wicket 10 033 */ 034public class ClassCryptFactory implements ICryptFactory 035{ 036 private static final Logger log = LoggerFactory.getLogger(ClassCryptFactory.class); 037 038 private final WeakReference<Class<?>> cryptClass; 039 private final String encryptionKey; 040 041 /** 042 * Construct. 043 * 044 * @param cryptClass 045 * class that will be instantiated to represent the ICrypt object 046 * @param encryptionKey 047 * encryption key 048 */ 049 public ClassCryptFactory(final Class<?> cryptClass, final String encryptionKey) 050 { 051 Args.notNull(cryptClass, "cryptClass"); 052 053 if (!ICrypt.class.isAssignableFrom(cryptClass)) 054 { 055 throw new IllegalArgumentException("cryptClass must implement ICrypt interface"); 056 } 057 058 this.cryptClass = new WeakReference<Class<?>>(cryptClass); 059 this.encryptionKey = encryptionKey; 060 } 061 062 @Override 063 public ICrypt newCrypt() 064 { 065 try 066 { 067 ICrypt crypt = (ICrypt)(cryptClass.get()).getDeclaredConstructor().newInstance(); 068 log.info("using encryption/decryption object {}", crypt); 069 crypt.setKey(encryptionKey); 070 return crypt; 071 } 072 catch (Exception e) 073 { 074 log.warn("************************** WARNING **************************"); 075 log.warn("As the instantiation of encryption/decryption class:"); 076 log.warn("\t" + cryptClass); 077 log.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.warn("Please override method org.apache.wicket.util.crypt.ICryptFactory.newCrypt()"); 081 log.warn("to provide a custom encryption/decryption implementation."); 082 log.warn("The cause of the instantiation failure: "); 083 log.warn("\t" + e.getMessage()); 084 if (log.isDebugEnabled()) 085 { 086 log.debug("exception: ", e); 087 } 088 else 089 { 090 log.warn("Set log level to DEBUG to display the stack trace."); 091 } 092 log.warn("*************************************************************"); 093 094 // assign the dummy crypt implementation 095 return new NoCrypt(); 096 } 097 } 098}