Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

still using a patched AJ, but without additional dependencies; the li…

…b reflections is out
  • Loading branch information...
commit 57baf9de3ae7f24af02a8091103ffcd8f2ad41af 1 parent 6ed5470
@florinpatrascu authored
View
37 activejdbc_m/AJ_patch.md
@@ -26,31 +26,24 @@ I had to patch AJ's `org.javalite.activejdbc.Model.ClassGetter` as an interim so
## New code
- static Reflections reflections = new Reflections("");
static class ClassGetter extends SecurityManager {
public String getClassName() {
- Set<Class<? extends Model>> classes =
- reflections.getSubTypesOf(Model.class);
-
- for (Class klass : classes) {
- if (Model.class.isAssignableFrom(klass)
- && klass != null && !klass.equals(Model.class)) {
- return klass.getName();
+ try {
+ Field f = ClassLoader.class.getDeclaredField("classes");
+ f.setAccessible(true);
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+
+ Vector<Class> classes = (Vector<Class>) f.get(cl);
+ for (Class clazz : classes) {
+ if (Model.class.isAssignableFrom(clazz) && clazz != null && !clazz.equals(Model.class)) {
+ return clazz.getName();
+ }
}
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
}
- throw new InitException( \
- "failed to determine Model class name, are you sure \
- models have been instrumented?");
+ throw new InitException("failed to determine Model class name, are you sure models have been instrumented?");
}
}
-
-the new code depends on the [Reflections](http://code.google.com/p/reflections/) library.
-
- <dependency>
- <groupId>org.reflections</groupId>
- <artifactId>reflections</artifactId>
- <version>0.9.8</version>
- </dependency>
-
-
-The dependencies will go away as soon as AJ can be used in its native form, provided the classloader issue is solved.
View
8 activejdbc_m/build.xml
@@ -172,18 +172,18 @@
</fileset>
</classpath>
</java>
+ <copy todir="${build.dir}/test/classes" >
+ <fileset dir="build/test/models" includes="**"/>
+ </copy>
<!--
- -->
- <!--<copy todir="${build.dir}/test/classes" >-->
- <!--<fileset dir="build/test/models" includes="**"/>-->
- <!--</copy>-->
<jar jarfile="${build.dist}/models.jar" basedir="${build.dir}/test/models">
<manifest>
<attribute name="Built-Date" value="${TODAY}"/>
</manifest>
</jar>
+ -->
</target>
View
104 activejdbc_m/src/ca/simplegames/micro/extensions/ActivejdbcExtension.java
@@ -17,15 +17,15 @@
package ca.simplegames.micro.extensions;
import ca.simplegames.micro.Extension;
+import ca.simplegames.micro.Globals;
import ca.simplegames.micro.SiteContext;
-import ca.simplegames.micro.extensions.activejdbc.ConnectionSpecWrapper;
-import org.javalite.activejdbc.ConnectionJdbcSpec;
-import org.javalite.activejdbc.DB;
+import com.jolbox.bonecp.BoneCPDataSource;
+import org.javalite.activejdbc.Base;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
-import java.util.*;
+import java.util.Map;
/**
* Adding support for the activejdbc library. Will set a global attribute that is available site wide
@@ -43,9 +43,8 @@
public static final String DEFAULT_DB_NAME = "default";
private String name;
private boolean debug;
- private static Map<String, List<ConnectionSpecWrapper>> connectionWrappers = new HashMap<String, List<ConnectionSpecWrapper>>();
private SiteContext site;
-
+ private BoneCPDataSource ds = null;
@SuppressWarnings("unchecked")
@Override
@@ -63,17 +62,26 @@ public Extension register(String name, SiteContext site, Map<String, Object> con
if (dbConfigFile.exists()) {
Map<String, Object> dbConfigForAll = (Map<String, Object>) new Yaml().load(new FileInputStream(dbConfigFile));
Map<String, Object> dbConfig = (Map<String, Object>) dbConfigForAll.get(site.getMicroEnv());
- ConnectionSpecWrapper jdbcConnectionWrapper = new ConnectionSpecWrapper();
//todo: check if jdbc/jndi and implement each case
if (dbConfig != null) {
- jdbcConnectionWrapper.setConnectionSpec(
- new ConnectionJdbcSpec(
- (String) dbConfig.get("driver"), (String) dbConfig.get("url"),
- (String) dbConfig.get("user"), (String) dbConfig.get("password")));
+ String driver = (String) dbConfig.get("driver");
+ Class.forName(driver);
+ ds = new BoneCPDataSource();
+ ds.setJdbcUrl((String) dbConfig.get("url"));
+ ds.setUsername((String) dbConfig.get("user"));
+ ds.setPassword(
+ dbConfig.get("password") != null ?
+ (String) dbConfig.get("password") : Globals.EMPTY_STRING);
+
+ Integer maxConnections = dbConfig.get("pool") != null ? (Integer) dbConfig.get("pool") : 5;
+
+ int minConnections = (int) Math.max(1, Math.ceil(maxConnections / 3)); // todo let the user configure it
+ ds.setMaxConnectionsPerPartition(minConnections);
+
+ ds.setMinConnectionsPerPartition(maxConnections / 3); // todo let the user configure it
+ ds.setPartitionCount(1);
- // todo add multiple database connections
- connectionWrappers.put(site.getMicroEnv(), Collections.singletonList(jdbcConnectionWrapper));
this.name = name;
} else {
throw new ExceptionInInitializerError(
@@ -92,74 +100,38 @@ public String getName() {
}
public void before() {
- before(null, false);
+ before(false);
}
- public void before(String dbName, boolean manageTransaction) {
- String dbNameWithDefault = dbName == null ? DEFAULT_DB_NAME : dbName;
- List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(dbNameWithDefault);
-
- for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) {
- DB db = new DB(connectionWrapper.getDbName());
- db.open(connectionWrapper.getConnectionSpec());
- if (manageTransaction) {
- db.openTransaction();
- }
+ public void before(boolean manageTransaction) {
+ Base.open(ds);
+ if (manageTransaction) {
+ Base.openTransaction();
}
-
}
public void after() {
- after(null, false);
+ after(false);
}
- public void after(String dbName, boolean manageTransaction) {
- String dbNameWithDefault = dbName == null ? DEFAULT_DB_NAME : dbName;
- List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(dbNameWithDefault);
- if (connectionWrappers != null && !connectionWrappers.isEmpty()) {
- for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) {
- DB db = new DB(connectionWrapper.getDbName());
- if (manageTransaction) {
- db.commitTransaction();
- }
- db.close();
- }
+ public void after(boolean manageTransaction) {
+ if (manageTransaction) {
+ Base.commitTransaction();
+ }
+ if (Base.hasConnection()) {
+ Base.close();
}
}
public void onException() {
- onException(null, false);
- }
-
- public void onException(String dbName, boolean manageTransaction) {
-
- String dbNameWithDefault = dbName == null ? DEFAULT_DB_NAME : dbName;
- List<ConnectionSpecWrapper> connectionWrappers = getConnectionWrappers(dbNameWithDefault);
-
- if (connectionWrappers != null && !connectionWrappers.isEmpty()) {
- for (ConnectionSpecWrapper connectionWrapper : connectionWrappers) {
- DB db = new DB(connectionWrapper.getDbName());
- if (manageTransaction) {
- db.rollbackTransaction();
- }
- db.close();
- }
- }
+ onException(false);
}
- /**
- * If dbName not provided, returns all connections which are not for testing.
- *
- * @return all the connections for a db name in a given environment
- */
- private List<ConnectionSpecWrapper> getConnectionWrappers(String dbName) {
- List<ConnectionSpecWrapper> result = new LinkedList<ConnectionSpecWrapper>();
-
- for (ConnectionSpecWrapper connectionWrapper : connectionWrappers.get(site.getMicroEnv())) {
- if ((dbName == null || dbName.equals(connectionWrapper.getDbName())))
- result.add(connectionWrapper);
+ public void onException(boolean manageTransaction) {
+ if (manageTransaction) {
+ Base.rollbackTransaction();
}
- return result;
+ Base.close();
}
public boolean isDebug() {
View
50 activejdbc_m/src/ca/simplegames/micro/extensions/activejdbc/ConnectionSpecWrapper.java
@@ -1,50 +0,0 @@
-/*
- * Copyright (c)2013 Florin T.Pătraşcu
- *
- * Licensed 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 ca.simplegames.micro.extensions.activejdbc;
-
-import org.javalite.activejdbc.ConnectionSpec;
-
-/**
- * adapted from activeweb's {@code org.javalite.activeweb.ConnectionSpecWrapper}
- *
- * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
- * @since $Revision$ (created: 2013-02-07 9:52 PM)
- */
-public class ConnectionSpecWrapper {
-
- private String dbName = "default";
- private ConnectionSpec connectionSpec;
-
- public ConnectionSpec getConnectionSpec() {
- return connectionSpec;
- }
-
- // in the original implementation, this is not a 'public' method
- public void setConnectionSpec(ConnectionSpec connectionSpec) {
- this.connectionSpec = connectionSpec;
- }
-
- public String getDbName() {
- return dbName;
- }
-
- void setDbName(String dbName) {
- if (dbName != null) {
- this.dbName = dbName;
- }
- }
-}
View
5 activejdbc_m/test/files/WEB-INF/controllers/FindArticles.bsh
@@ -22,11 +22,10 @@ try{
AJ.before();
articles = Article.findAll();
- context.put("articles", articles);
-
+ context.put("totalArticles", articles.size());
+ context.put("article", articles.get(0).toMap());
}catch(e){
e.printStackTrace();
- log.error(e.Message);
AJ.onException();
}finally{
AJ.after();
View
4 activejdbc_m/test/files/WEB-INF/views/content/index.html
@@ -1,3 +1,3 @@
<h1 id="welcome">Welcome!</h1>
-Articles:<br/>
-$articles
+Total Articles: $totalArticles<br/>
+$article
View
4 activejdbc_m/test/src/ActivejdbcTest.java
@@ -81,7 +81,9 @@ public void testMicroContent() throws Exception {
.with(Rack.PATH_INFO, "/index.html");
RackResponse response = micro.call(input);
- Assert.assertTrue(RackResponse.getBodyAsString(response).contains("Welcome!"));
+ final String bodyAsString = RackResponse.getBodyAsString(response);
+ Assert.assertTrue(bodyAsString.contains("Total Articles: 2"));
+ Assert.assertTrue(bodyAsString.contains("title=activejdbc_m"));
}
}
View
BIN  activejdbc_m/test/test.h2.db
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.