mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
initial add of EbeanORM server based on v2.8.1
This commit is contained in:
1 parent
2b74d0f9d9
commit
96ce4c0ddf
1188 files changed
+135034
No files matched your search
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Copyright (C) 2009 Authors
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
/**
|
||||
* Stack based on ArrayList.
|
||||
*
|
||||
* @author rbygrave
|
||||
*/
|
||||
public class ArrayStack<E> {
|
||||
|
||||
private final ArrayList<E> list;
|
||||
|
||||
/**
|
||||
* Creates an empty Stack with an initial size.
|
||||
*/
|
||||
public ArrayStack(int size) {
|
||||
this.list = new ArrayList<E>(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty Stack.
|
||||
*/
|
||||
public ArrayStack() {
|
||||
this.list = new ArrayList<E>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes an item onto the top of this stack.
|
||||
*/
|
||||
public E push(E item) {
|
||||
list.add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the object at the top of this stack and returns that object as
|
||||
* the value of this function.
|
||||
*/
|
||||
public E pop() {
|
||||
int len = list.size();
|
||||
E obj = peek();
|
||||
list.remove(len - 1);
|
||||
return obj;
|
||||
}
|
||||
|
||||
protected E peekZero(boolean retNull) {
|
||||
int len = list.size();
|
||||
if (len == 0) {
|
||||
if (retNull) {
|
||||
return null;
|
||||
}
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
return list.get(len - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object at the top of this stack without removing it.
|
||||
*/
|
||||
public E peek() {
|
||||
return peekZero(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object at the top of this stack without removing it.
|
||||
* If the stack is empty this returns null.
|
||||
*/
|
||||
public E peekWithNull() {
|
||||
return peekZero(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if this stack is empty.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return list.isEmpty();
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public boolean contains(Object o){
|
||||
return list.contains(o);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
import com.avaje.ebean.bean.BeanCollection;
|
||||
import com.avaje.ebean.common.BeanList;
|
||||
import com.avaje.ebean.common.BeanMap;
|
||||
import com.avaje.ebean.common.BeanSet;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
|
||||
/**
|
||||
* Creates the BeanCollections.
|
||||
* <p>
|
||||
* Creates the BeanSet BeanMap and BeanList objects.
|
||||
* </p>
|
||||
*/
|
||||
public class BeanCollectionFactory {
|
||||
|
||||
private static class BeanCollectionFactoryHolder {
|
||||
private static BeanCollectionFactory me = new BeanCollectionFactory();
|
||||
}
|
||||
|
||||
private static final int defaultListInitialCapacity = 20;
|
||||
private static final int defaultSetInitialCapacity = 32;
|
||||
private static final int defaultMapInitialCapacity = 32;
|
||||
|
||||
private BeanCollectionFactory() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a BeanCollection for the given parameters.
|
||||
*/
|
||||
public static BeanCollection<?> create(BeanCollectionParams params) {
|
||||
return BeanCollectionFactoryHolder.me.createMany(params);
|
||||
}
|
||||
|
||||
|
||||
private BeanCollection<?> createMany(BeanCollectionParams params) {
|
||||
|
||||
SpiQuery.Type manyType = params.getManyType();
|
||||
switch (manyType) {
|
||||
case MAP:
|
||||
return createMap(params);
|
||||
case LIST:
|
||||
return createList(params);
|
||||
case SET:
|
||||
return createSet(params);
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Invalid Arg " + manyType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private BeanMap createMap(BeanCollectionParams params) {
|
||||
|
||||
return new BeanMap(new LinkedHashMap(defaultMapInitialCapacity));
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private BeanSet createSet(BeanCollectionParams params) {
|
||||
|
||||
return new BeanSet(new LinkedHashSet(defaultSetInitialCapacity));
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private BeanList createList(BeanCollectionParams params) {
|
||||
|
||||
return new BeanList(new ArrayList(defaultListInitialCapacity));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
|
||||
/**
|
||||
* Parameters used to create the specific Map Set or List object.
|
||||
*/
|
||||
public class BeanCollectionParams {
|
||||
|
||||
private final SpiQuery.Type manyType;
|
||||
|
||||
/**
|
||||
* Construct without a specific capacity.
|
||||
*/
|
||||
public BeanCollectionParams(SpiQuery.Type manyType) {
|
||||
this.manyType = manyType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type Map Set or List.
|
||||
*/
|
||||
public SpiQuery.Type getManyType() {
|
||||
return manyType;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.config.EncryptKey;
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import com.avaje.ebeaninternal.api.BindParams.OrderedList;
|
||||
import com.avaje.ebeaninternal.api.BindParams.Param;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
|
||||
/**
|
||||
* Parses the BindParams if they are using named parameters.
|
||||
* <p>
|
||||
* This is a thread safe implementation.
|
||||
* </p>
|
||||
*/
|
||||
public class BindParamsParser {
|
||||
|
||||
|
||||
public static final String ENCRYPTKEY_PREFIX = "encryptkey_";
|
||||
public static final String ENCRYPTKEY_GAP = "___";
|
||||
|
||||
private static final int ENCRYPTKEY_PREFIX_LEN = ENCRYPTKEY_PREFIX.length();
|
||||
private static final int ENCRYPTKEY_GAP_LEN = ENCRYPTKEY_GAP.length();
|
||||
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String quote = "'";
|
||||
|
||||
/**
|
||||
* Used to parse sql looking for named parameters.
|
||||
*/
|
||||
private static final String colon = ":";
|
||||
|
||||
private final BindParams params;
|
||||
private final String sql;
|
||||
|
||||
private final BeanDescriptor<?> beanDescriptor;
|
||||
|
||||
public static String parse(BindParams params, String sql) {
|
||||
return parse(params, sql, null);
|
||||
}
|
||||
|
||||
public static String parse(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
return new BindParamsParser(params, sql, beanDescriptor).parseSql();
|
||||
}
|
||||
|
||||
public static OrderedList parseNamedParams(BindParams params, String sql) {
|
||||
return new BindParamsParser(params, sql, null).parseSqlNamedParams();
|
||||
}
|
||||
|
||||
private BindParamsParser(BindParams params, String sql, BeanDescriptor<?> beanDescriptor) {
|
||||
this.params = params;
|
||||
this.sql = sql;
|
||||
this.beanDescriptor = beanDescriptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for parsing having clauses with named parameters.
|
||||
* <p>
|
||||
* The issue here is that BindParams contains named parameters for
|
||||
* both where and having clauses. BindParams.positionedParameters is
|
||||
* used for the where and the OrderedList for the having.
|
||||
* </p>
|
||||
*/
|
||||
private OrderedList parseSqlNamedParams() {
|
||||
OrderedList orderedList = new OrderedList();
|
||||
parseNamedParams(orderedList);
|
||||
return orderedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the sql changed named parameters to positioned parameters if required.
|
||||
* <p>
|
||||
* The sql is used when named parameters are used.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is used in most cases of named parameters. The case it is NOT used for is
|
||||
* named parameters in a having clause. In this case some of the named parameters
|
||||
* could be for a where clause and some for the having clause.
|
||||
* </p>
|
||||
*/
|
||||
private String parseSql() {
|
||||
|
||||
String preparedSql = params.getPreparedSql();
|
||||
if (preparedSql != null && preparedSql.length() > 0){
|
||||
// the sql has already been parsed and
|
||||
// positionedParameters are set in order
|
||||
return preparedSql;
|
||||
}
|
||||
|
||||
String prepardSql;
|
||||
if (params.requiresNamedParamsPrepare()) {
|
||||
OrderedList orderedList = new OrderedList(params.positionedParameters());
|
||||
|
||||
parseNamedParams(orderedList);
|
||||
prepardSql = orderedList.getPreparedSql();
|
||||
} else {
|
||||
prepardSql = sql;
|
||||
}
|
||||
params.setPreparedSql(prepardSql);
|
||||
return prepardSql;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Named parameters need to be parsed and replaced with ?.
|
||||
*/
|
||||
private void parseNamedParams(OrderedList orderedList) {
|
||||
|
||||
parseNamedParams(0, orderedList);
|
||||
}
|
||||
|
||||
private void parseNamedParams(int startPos, OrderedList orderedList) {
|
||||
|
||||
if (sql == null){
|
||||
throw new PersistenceException("query does not contain any named bind parameters?");
|
||||
}
|
||||
if (startPos > sql.length()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// search for quotes and named params... in order...
|
||||
int beginQuotePos = sql.indexOf(quote, startPos);
|
||||
int nameParamStart = sql.indexOf(colon, startPos);
|
||||
if (beginQuotePos > 0 && beginQuotePos < nameParamStart) {
|
||||
// the quote precedes the named parameter...
|
||||
// find and add up to the end quote
|
||||
int endQuotePos = sql.indexOf(quote, beginQuotePos + 1);
|
||||
String sub = sql.substring(startPos, endQuotePos + 1);
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
// start again after the end quote
|
||||
parseNamedParams(endQuotePos + 1, orderedList);
|
||||
|
||||
} else {
|
||||
if (nameParamStart < 0) {
|
||||
// no more params, add the rest
|
||||
String sub = sql.substring(startPos, sql.length());
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
} else {
|
||||
// find the end of the parameter name
|
||||
int endOfParam = nameParamStart + 1;
|
||||
do {
|
||||
char c = sql.charAt(endOfParam);
|
||||
if (c != '_' && !Character.isLetterOrDigit(c)) {
|
||||
break;
|
||||
}
|
||||
endOfParam++;
|
||||
} while (endOfParam < sql.length());
|
||||
|
||||
// add the named parameter value to bindList
|
||||
String paramName = sql.substring(nameParamStart + 1, endOfParam);
|
||||
|
||||
Param param;
|
||||
if (paramName.startsWith(ENCRYPTKEY_PREFIX)){
|
||||
param = addEncryptKeyParam(paramName);
|
||||
} else {
|
||||
param = params.getParameter(paramName);
|
||||
}
|
||||
|
||||
if (param == null) {
|
||||
String msg = "Bind value is not set or null for [" + paramName
|
||||
+ "] in [" + sql+ "]";
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
|
||||
String sub = sql.substring(startPos, nameParamStart);
|
||||
orderedList.appendSql(sub);
|
||||
|
||||
// check if inValue is a Collection type...
|
||||
Object inValue = param.getInValue();
|
||||
if (inValue != null && inValue instanceof Collection<?>){
|
||||
// Chop up Collection parameter into a number
|
||||
// of individual parameters and add each one individually
|
||||
Collection<?> collection = (Collection<?>)inValue;
|
||||
Iterator<?> it = collection.iterator();
|
||||
int c = 0;
|
||||
while (it.hasNext()) {
|
||||
Object elVal = (Object) it.next();
|
||||
if (++c > 1){
|
||||
orderedList.appendSql(",");
|
||||
}
|
||||
orderedList.appendSql("?");
|
||||
BindParams.Param elParam = new BindParams.Param();
|
||||
elParam.setInValue(elVal);
|
||||
orderedList.add(elParam);
|
||||
}
|
||||
|
||||
} else {
|
||||
// its a normal scalar value parameter...
|
||||
orderedList.add(param);
|
||||
orderedList.appendSql("?");
|
||||
}
|
||||
|
||||
// continue on after the end of the parameter
|
||||
parseNamedParams(endOfParam, orderedList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an encryption key bind parameter.
|
||||
*/
|
||||
private Param addEncryptKeyParam(String keyNamedParam) {
|
||||
|
||||
|
||||
int pos = keyNamedParam.indexOf(ENCRYPTKEY_GAP, ENCRYPTKEY_PREFIX_LEN);
|
||||
|
||||
String tableName = keyNamedParam.substring(ENCRYPTKEY_PREFIX_LEN, pos);
|
||||
String columnName = keyNamedParam.substring(pos+ENCRYPTKEY_GAP_LEN);
|
||||
|
||||
EncryptKey key = beanDescriptor.getEncryptKey(tableName, columnName);
|
||||
String strKey = key.getStringValue();
|
||||
|
||||
return params.setEncryptionKey(keyNamedParam, strKey);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
/**
|
||||
* Get the paths used to search for entity beans etc.
|
||||
* <p>
|
||||
* Typically this will return a URL[] of the classpath.
|
||||
* </p>
|
||||
*/
|
||||
public interface ClassPathReader {
|
||||
|
||||
/**
|
||||
* Return the paths in the classpath to search for entity beans etc.
|
||||
* <p>
|
||||
* This will typically return a URL[] or String[] of the entries in the
|
||||
* class path.
|
||||
* </p>
|
||||
*/
|
||||
public Object[] readPath(ClassLoader classLoader);
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.avaje.ebean.config.GlobalProperties;
|
||||
import com.avaje.ebeaninternal.api.ClassUtil;
|
||||
|
||||
/**
|
||||
* Can search the class path for classes using a ClassPathSearchMatcher. A
|
||||
* ClassPathSearch should only be used once in a single threaded manor. It is
|
||||
* not safe for multithreaded use.
|
||||
* <p>
|
||||
* For example, used to find all the Entity beans and ScalarTypes for Ebean.
|
||||
* </p>
|
||||
*/
|
||||
public class ClassPathSearch {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ClassPathSearch.class.getName());
|
||||
|
||||
ClassLoader classLoader;
|
||||
|
||||
Object[] classPaths;
|
||||
|
||||
ClassPathSearchFilter filter;
|
||||
|
||||
ClassPathSearchMatcher matcher;
|
||||
|
||||
ArrayList<Class<?>> matchList = new ArrayList<Class<?>>();
|
||||
|
||||
HashSet<String> jarHits = new HashSet<String>();
|
||||
|
||||
HashSet<String> packageHits = new HashSet<String>();
|
||||
|
||||
ClassPathReader classPathReader = new DefaultClassPathReader();
|
||||
|
||||
public ClassPathSearch(ClassLoader classLoader, ClassPathSearchFilter filter, ClassPathSearchMatcher matcher) {
|
||||
this.classLoader = classLoader;
|
||||
this.filter = filter;
|
||||
this.matcher = matcher;
|
||||
initClassPaths();
|
||||
}
|
||||
|
||||
private void initClassPaths() {
|
||||
|
||||
try {
|
||||
|
||||
String cn = GlobalProperties.get("ebean.classpathreader", null);
|
||||
if (cn != null){
|
||||
// use a user defined classPathReader
|
||||
logger.info("Using ["+cn+"] to read the searchable class path");
|
||||
classPathReader = (ClassPathReader)ClassUtil.newInstance(cn, this.getClass());
|
||||
}
|
||||
|
||||
classPaths = classPathReader.readPath(classLoader);
|
||||
|
||||
if (classPaths == null || classPaths.length == 0){
|
||||
String msg = "ClassPath is EMPTY using ClassPathReader ["+classPathReader+"]";
|
||||
logger.warning(msg);
|
||||
}
|
||||
|
||||
boolean debug = GlobalProperties.getBoolean("ebean.debug.classpath", false);
|
||||
if (debug || logger.isLoggable(Level.FINER)) {
|
||||
String msg = "Classpath " + Arrays.toString(classPaths);
|
||||
logger.info(msg);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
String msg = "Error trying to read the classpath entries";
|
||||
throw new RuntimeException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set of jars that contained classes that matched.
|
||||
*/
|
||||
public Set<String> getJarHits() {
|
||||
return jarHits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set of packages that contained classes that matched.
|
||||
*/
|
||||
public Set<String> getPackageHits() {
|
||||
return packageHits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register where matching classes where found.
|
||||
* <p>
|
||||
* Could use this info to speed up future searches.
|
||||
* </p>
|
||||
*/
|
||||
private void registerHit(String jarFileName, Class<?> cls) {
|
||||
if (jarFileName != null) {
|
||||
jarHits.add(jarFileName);
|
||||
}
|
||||
Package pkg = cls.getPackage();
|
||||
if (pkg != null){
|
||||
packageHits.add(pkg.getName());
|
||||
} else {
|
||||
packageHits.add("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the class path for all matching classes.
|
||||
*/
|
||||
public List<Class<?>> findClasses() throws ClassNotFoundException {
|
||||
|
||||
if (classPaths == null || classPaths.length == 0){
|
||||
// returning an empty list
|
||||
return matchList;
|
||||
}
|
||||
|
||||
String charsetName = Charset.defaultCharset().name();
|
||||
|
||||
for (int h = 0; h < classPaths.length; h++) {
|
||||
|
||||
String jarFileName = null;
|
||||
Enumeration<?> files = null;
|
||||
JarFile module = null;
|
||||
|
||||
// for each class path ...
|
||||
File classPath;
|
||||
if (URL.class.isInstance(classPaths[h])){
|
||||
classPath = new File(((URL)classPaths[h]).getFile());
|
||||
} else {
|
||||
classPath = new File(classPaths[h].toString());
|
||||
}
|
||||
|
||||
try {
|
||||
// URL Decode the path replacing %20 to space characters.
|
||||
String path = URLDecoder.decode(classPath.getAbsolutePath(), charsetName);
|
||||
classPath = new File(path);
|
||||
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
if (classPath.isDirectory()) {
|
||||
files = getDirectoryEnumeration(classPath);
|
||||
|
||||
} else if (classPath.getName().endsWith(".jar")) {
|
||||
jarFileName = classPath.getName();
|
||||
if (!filter.isSearchJar(jarFileName)) {
|
||||
// skip any jars not list in the filter
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
// our resource is a jar
|
||||
module = new JarFile(classPath);
|
||||
files = module.entries();
|
||||
|
||||
} catch (MalformedURLException ex) {
|
||||
throw new ClassNotFoundException("Bad classpath. Error: ", ex);
|
||||
|
||||
} catch (IOException ex) {
|
||||
String msg = "jar file '" + classPath.getAbsolutePath()
|
||||
+ "' could not be instantiate from file path. Error: ";
|
||||
throw new ClassNotFoundException(msg, ex);
|
||||
}
|
||||
} else {
|
||||
// this is not expected
|
||||
String msg = "Error: expected classPath entry ["+classPath.getAbsolutePath()
|
||||
+"] to be a directory or a .jar file but it is not either of those?";
|
||||
logger.log(Level.SEVERE, msg);
|
||||
}
|
||||
|
||||
searchFiles(files, jarFileName);
|
||||
|
||||
if (module != null) {
|
||||
try {
|
||||
// close the jar if it was used
|
||||
module.close();
|
||||
} catch (IOException e) {
|
||||
String msg = "Error closing jar";
|
||||
throw new ClassNotFoundException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchList.isEmpty()){
|
||||
String msg = "No Entities found in ClassPath using ClassPathReader ["
|
||||
+classPathReader+"] Classpath Searched[" + Arrays.toString(classPaths)+"]";
|
||||
logger.warning(msg);
|
||||
}
|
||||
|
||||
return matchList;
|
||||
}
|
||||
|
||||
private Enumeration<?> getDirectoryEnumeration(File classPath) {
|
||||
|
||||
// list of file names (latter checked as Classes)
|
||||
ArrayList<String> fileNameList = new ArrayList<String>();
|
||||
|
||||
Set<String> includePkgs = filter.getIncludePackages();
|
||||
if (includePkgs.size() > 0) {
|
||||
// just search the relevant directories based on the
|
||||
// list of included packages
|
||||
Iterator<String> it = includePkgs.iterator();
|
||||
while (it.hasNext()) {
|
||||
String pkg = it.next();
|
||||
String relPath = pkg.replace('.', '/');
|
||||
File dir = new File(classPath, relPath);
|
||||
if (dir.exists()) {
|
||||
recursivelyListDir(fileNameList, dir, new StringBuilder(relPath));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// get a recursive listing of this classpath
|
||||
recursivelyListDir(fileNameList, classPath, new StringBuilder());
|
||||
}
|
||||
|
||||
return Collections.enumeration(fileNameList);
|
||||
}
|
||||
|
||||
private void searchFiles(Enumeration<?> files, String jarFileName) {
|
||||
|
||||
while (files != null && files.hasMoreElements()) {
|
||||
|
||||
String fileName = files.nextElement().toString();
|
||||
|
||||
// we only want the class files
|
||||
if (fileName.endsWith(".class")) {
|
||||
|
||||
String className = fileName.replace('/', '.').substring(0, fileName.length() - 6);
|
||||
int lastPeriod = className.lastIndexOf(".");
|
||||
|
||||
String pckgName;
|
||||
if (lastPeriod > 0){
|
||||
pckgName = className.substring(0, lastPeriod);
|
||||
} else {
|
||||
pckgName = "";
|
||||
}
|
||||
|
||||
if (!filter.isSearchPackage(pckgName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// get the class for our class name
|
||||
Class<?> theClass = null;
|
||||
try {
|
||||
theClass = Class.forName(className, false, classLoader);
|
||||
|
||||
if (matcher.isMatch(theClass)) {
|
||||
matchList.add(theClass);
|
||||
registerHit(jarFileName, theClass);
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
// expected to get this hence finer
|
||||
logger.finer("Error searching classpath" + e.getMessage());
|
||||
continue;
|
||||
|
||||
} catch (NoClassDefFoundError e) {
|
||||
// expected to get this hence finer
|
||||
logger.finer("Error searching classpath: " + e.getMessage());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recursivelyListDir(List<String> fileNameList, File dir, StringBuilder relativePath) {
|
||||
|
||||
int prevLen;
|
||||
|
||||
if (dir.isDirectory()) {
|
||||
File[] files = dir.listFiles();
|
||||
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
// store our original relative path string length
|
||||
prevLen = relativePath.length();
|
||||
relativePath.append(prevLen == 0 ? "" : "/").append(files[i].getName());
|
||||
|
||||
recursivelyListDir(fileNameList, files[i], relativePath);
|
||||
|
||||
// delete sub directory from our relative path
|
||||
relativePath.delete(prevLen, relativePath.length());
|
||||
}
|
||||
} else {
|
||||
// add class fileName to the list
|
||||
fileNameList.add(relativePath.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Used to reduce the classes searched by excluding jars and packages.
|
||||
*/
|
||||
public class ClassPathSearchFilter {
|
||||
|
||||
private static final String COM_AVAJE_EBEANINTERNAL_SERVER_BEAN = "com.avaje.ebeaninternal.server.bean";
|
||||
|
||||
private static final String COM_AVAJE_EBEAN_META = "com.avaje.ebean.meta";
|
||||
|
||||
private boolean defaultPackageMatch = true;
|
||||
|
||||
private boolean defaultJarMatch = false;
|
||||
|
||||
private String ebeanJarPrefix = "ebean";
|
||||
|
||||
private HashSet<String> includePackageSet = new HashSet<String>();
|
||||
|
||||
private HashSet<String> excludePackageSet = new HashSet<String>();
|
||||
|
||||
private HashSet<String> includeJarSet = new HashSet<String>();
|
||||
|
||||
private HashSet<String> excludeJarSet = new HashSet<String>();
|
||||
|
||||
public ClassPathSearchFilter() {
|
||||
addDefaultExcludePackages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the ebean jar file. Used with class path search to find
|
||||
* the "Meta" entity beans that are contained within the ebean jar.
|
||||
* <p>
|
||||
* You only need to set this if the ebean jar file name starts with
|
||||
* something other than ebean.
|
||||
* </p>
|
||||
*/
|
||||
public void setEbeanJarPrefix(String ebeanJarPrefix) {
|
||||
this.ebeanJarPrefix = ebeanJarPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the explicit packages that should be searched.
|
||||
*/
|
||||
public Set<String> getIncludePackages() {
|
||||
return includePackageSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some packages which by default will be excluded from a search.
|
||||
* <p>
|
||||
* This includes java, javax, etc.
|
||||
* </p>
|
||||
* <p>
|
||||
* This is not used when the includePackages is set, but can speed a search
|
||||
* when includePackages has not been set.
|
||||
* </p>
|
||||
*/
|
||||
public void addDefaultExcludePackages() {
|
||||
excludePackage("sun");
|
||||
excludePackage("com.sun");
|
||||
excludePackage("java");
|
||||
excludePackage("javax");
|
||||
excludePackage("junit");
|
||||
excludePackage("org.w3c");
|
||||
excludePackage("org.xml");
|
||||
excludePackage("org.apache");
|
||||
excludePackage("com.mysql");
|
||||
excludePackage("oracle.jdbc");
|
||||
excludePackage("com.microsoft.sqlserver");
|
||||
excludePackage("com.avaje.ebean");
|
||||
excludePackage("com.avaje.lib");
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all entries from the exclude packages list.
|
||||
* <p>
|
||||
* This includes the entries added by addDefaultExcludePackages() which is
|
||||
* done on construction.
|
||||
* </p>
|
||||
*/
|
||||
public void clearExcludePackages() {
|
||||
excludePackageSet.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default for jar matching when a jar is neither explicitly
|
||||
* included or excluded.
|
||||
*/
|
||||
public void setDefaultJarMatch(boolean defaultJarMatch) {
|
||||
this.defaultJarMatch = defaultJarMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default for package matching when a package is neither explicitly
|
||||
* included or excluded.
|
||||
*/
|
||||
public void setDefaultPackageMatch(boolean defaultPackageMatch) {
|
||||
this.defaultPackageMatch = defaultPackageMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a package to explicitly include in the search.
|
||||
*/
|
||||
public void includePackage(String pckgName) {
|
||||
includePackageSet.add(pckgName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a package to explicitly exclude in the search.
|
||||
*/
|
||||
public void excludePackage(String pckgName) {
|
||||
excludePackageSet.add(pckgName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a jar to explicitly exclude in the search.
|
||||
*/
|
||||
public void excludeJar(String jarName) {
|
||||
includeJarSet.add(jarName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a jar to explicitly include in the search.
|
||||
*/
|
||||
public void includeJar(String jarName) {
|
||||
includeJarSet.add(jarName);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the package should be included in the search.
|
||||
*/
|
||||
public boolean isSearchPackage(String packageName) {
|
||||
// special case... "meta" entity beans.
|
||||
if (COM_AVAJE_EBEAN_META.equals(packageName)) {
|
||||
return true;
|
||||
}
|
||||
// special case... BeanFinders etc for "meta" beans.
|
||||
if (COM_AVAJE_EBEANINTERNAL_SERVER_BEAN.equals(packageName)) {
|
||||
return true;
|
||||
}
|
||||
if (includePackageSet != null && !includePackageSet.isEmpty()){
|
||||
return containedIn(includePackageSet, packageName);
|
||||
}
|
||||
if (containedIn(excludePackageSet, packageName)) {
|
||||
return false;
|
||||
}
|
||||
return defaultPackageMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the jar should be included in the search.
|
||||
*/
|
||||
public boolean isSearchJar(String jarName) {
|
||||
if (jarName.startsWith(ebeanJarPrefix)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (containedIn(includeJarSet, jarName)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (containedIn(excludeJarSet, jarName)) {
|
||||
return false;
|
||||
}
|
||||
return defaultJarMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to determine is a match is contained in the set.
|
||||
*/
|
||||
protected boolean containedIn(HashSet<String> set, String match) {
|
||||
if (set.contains(match)) {
|
||||
return true;
|
||||
}
|
||||
Iterator<String> incIt = set.iterator();
|
||||
while (incIt.hasNext()) {
|
||||
String val = incIt.next();
|
||||
if (match.startsWith(val)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
/**
|
||||
* Defines interface for finding classes via a class path search.
|
||||
*/
|
||||
public interface ClassPathSearchMatcher {
|
||||
|
||||
/**
|
||||
* Return true if the class matches the specific search.
|
||||
* <p>
|
||||
* Note that the location in terms of jars and packages is noted and can be
|
||||
* used to make future searches faster.
|
||||
* </p>
|
||||
*/
|
||||
public boolean isMatch(Class<?> cls);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Default implementation for getting the classPath from the classLoader.
|
||||
* <p>
|
||||
* This class path is used to search for entity beans etc.
|
||||
* </p>
|
||||
*/
|
||||
public class DefaultClassPathReader implements ClassPathReader {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(DefaultClassPathReader.class.getName());
|
||||
|
||||
public Object[] readPath(ClassLoader classLoader) {
|
||||
|
||||
if (classLoader instanceof URLClassLoader){
|
||||
// this is really what we are hoping for
|
||||
URLClassLoader ucl = (URLClassLoader)classLoader;
|
||||
return ucl.getURLs();
|
||||
}
|
||||
|
||||
try {
|
||||
// search for a "getClassPath" method... resin2
|
||||
Method method = classLoader.getClass().getMethod("getClassPath");
|
||||
if (method != null){
|
||||
logger.info("Using getClassPath() method on classLoader["+classLoader.getClass()+"]");
|
||||
String s = method.invoke(classLoader).toString();
|
||||
return s.split(File.pathSeparator);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
// Not really an error...
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unexpected Error trying to read classpath from classloader", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// search for a "getClasspath" method... Ant
|
||||
Method method = classLoader.getClass().getMethod("getClasspath");
|
||||
if (method != null){
|
||||
logger.info("Using getClasspath() method on classLoader["+classLoader.getClass()+"]");
|
||||
String s = method.invoke(classLoader).toString();
|
||||
|
||||
return s.split(File.pathSeparator);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
// Not really an error...
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unexpected Error trying to read classpath from classloader", e);
|
||||
}
|
||||
|
||||
String imsg = "Unsure how to read classpath from classLoader ["+classLoader.getClass()+"]";
|
||||
logger.info(imsg);
|
||||
|
||||
String msg = "Using java.class.path system property to search for entity beans";
|
||||
logger.warning(msg);
|
||||
|
||||
return System.getProperty("java.class.path", "").split(File.pathSeparator);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* This file is part of Ebean.
|
||||
*
|
||||
* Ebean is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Ebean is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.util;
|
||||
|
||||
/**
|
||||
* Helper for checking the state of the application is correct.
|
||||
*/
|
||||
public class InternalAssert {
|
||||
|
||||
/**
|
||||
* Throws an IllegalStateException if o is null.
|
||||
*/
|
||||
public static void notNull(Object o, String msg){
|
||||
if (o == null){
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an IllegalStateException if b is not true.
|
||||
*/
|
||||
public static void isTrue(boolean b, String msg){
|
||||
if (!b){
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
|
||||
<TITLE>Server side Utility objects</TITLE>
|
||||
</HEAD>
|
||||
<Body BGCOLOR="#ffffff">
|
||||
Server side Utility objects
|
||||
|
||||
|
||||
|
||||
</Body>
|
||||
</HTML>
|
||||
Reference in new issue
Block a user