From 63e9a82b9807e074504bba1d6b795c75b37e405a Mon Sep 17 00:00:00 2001 From: crazyboblee Date: Mon, 26 Feb 2007 03:08:16 +0000 Subject: Simplified source directory structure. Forget Maven. git-svn-id: https://google-guice.googlecode.com/svn/trunk@227 d779f126-a31b-0410-b53b-1d3aecad763e --- struts2/example/build.properties | 3 +- .../com/google/inject/struts2/example/Count.java | 39 +++++ .../com/google/inject/struts2/example/Counter.java | 33 ++++ .../inject/struts2/example/ExampleModule.java | 30 ++++ .../com/google/inject/struts2/example/Main.java | 42 +++++ .../com/google/inject/struts2/example/Count.java | 39 ----- .../com/google/inject/struts2/example/Counter.java | 33 ---- .../inject/struts2/example/ExampleModule.java | 30 ---- .../com/google/inject/struts2/example/Main.java | 42 ----- struts2/plugin/build.properties | 2 +- .../google/inject/struts2/GuiceObjectFactory.java | 176 +++++++++++++++++++++ .../google/inject/struts2/GuiceObjectFactory.java | 176 --------------------- struts2/plugin/src/main/java/struts-plugin.xml | 16 -- struts2/plugin/src/struts-plugin.xml | 16 ++ 14 files changed, 338 insertions(+), 339 deletions(-) create mode 100644 struts2/example/src/com/google/inject/struts2/example/Count.java create mode 100644 struts2/example/src/com/google/inject/struts2/example/Counter.java create mode 100644 struts2/example/src/com/google/inject/struts2/example/ExampleModule.java create mode 100644 struts2/example/src/com/google/inject/struts2/example/Main.java delete mode 100644 struts2/example/src/main/java/com/google/inject/struts2/example/Count.java delete mode 100644 struts2/example/src/main/java/com/google/inject/struts2/example/Counter.java delete mode 100644 struts2/example/src/main/java/com/google/inject/struts2/example/ExampleModule.java delete mode 100644 struts2/example/src/main/java/com/google/inject/struts2/example/Main.java create mode 100644 struts2/plugin/src/com/google/inject/struts2/GuiceObjectFactory.java delete mode 100644 struts2/plugin/src/main/java/com/google/inject/struts2/GuiceObjectFactory.java delete mode 100644 struts2/plugin/src/main/java/struts-plugin.xml create mode 100644 struts2/plugin/src/struts-plugin.xml (limited to 'struts2') diff --git a/struts2/example/build.properties b/struts2/example/build.properties index f750b501..a155282f 100644 --- a/struts2/example/build.properties +++ b/struts2/example/build.properties @@ -1,4 +1,3 @@ lib.dir=../lib -src.dir=src/main/java -test.dir=src/test/java +src.dir=src build.dir=build diff --git a/struts2/example/src/com/google/inject/struts2/example/Count.java b/struts2/example/src/com/google/inject/struts2/example/Count.java new file mode 100644 index 00000000..4fca1516 --- /dev/null +++ b/struts2/example/src/com/google/inject/struts2/example/Count.java @@ -0,0 +1,39 @@ +/** + * Copyright (C) 2006 Google Inc. + * + * 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 com.google.inject.struts2.example; + +import com.google.inject.Inject; + +import static com.opensymphony.xwork2.Action.SUCCESS; + +public class Count { + + final Counter counter; + + @Inject + public Count(Counter counter) { + this.counter = counter; + } + + public String execute() { + return SUCCESS; + } + + public int getCount() { + return counter.increment(); + } +} diff --git a/struts2/example/src/com/google/inject/struts2/example/Counter.java b/struts2/example/src/com/google/inject/struts2/example/Counter.java new file mode 100644 index 00000000..c375b981 --- /dev/null +++ b/struts2/example/src/com/google/inject/struts2/example/Counter.java @@ -0,0 +1,33 @@ +/** + * Copyright (C) 2006 Google Inc. + * + * 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 com.google.inject.struts2.example; + +import com.google.inject.servlet.SessionScoped; + +/** + * Session-scoped counter. + */ +@SessionScoped +public class Counter { + + int count = 0; + + /** Increments the count and returns the new value. */ + public synchronized int increment() { + return count++; + } +} diff --git a/struts2/example/src/com/google/inject/struts2/example/ExampleModule.java b/struts2/example/src/com/google/inject/struts2/example/ExampleModule.java new file mode 100644 index 00000000..b8a04f7e --- /dev/null +++ b/struts2/example/src/com/google/inject/struts2/example/ExampleModule.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2006 Google Inc. + * + * 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 com.google.inject.struts2.example; + +import com.google.inject.AbstractModule; + +/** + * Example application module. + * + * @author crazybob@google.com (Bob Lee) + */ +public class ExampleModule extends AbstractModule { + + protected void configure() { + } +} diff --git a/struts2/example/src/com/google/inject/struts2/example/Main.java b/struts2/example/src/com/google/inject/struts2/example/Main.java new file mode 100644 index 00000000..60fd0bec --- /dev/null +++ b/struts2/example/src/com/google/inject/struts2/example/Main.java @@ -0,0 +1,42 @@ +/** + * Copyright (C) 2006 Google Inc. + * + * 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 com.google.inject.struts2.example; + +import org.mortbay.jetty.Connector; +import org.mortbay.jetty.Server; +import org.mortbay.jetty.webapp.WebAppContext; +import org.mortbay.jetty.nio.SelectChannelConnector; + +/** + * Starts the example web server on port 8080. Run from "./struts2/example". + */ +public class Main { + + public static void main(String[] args) throws Exception { + Server server = new Server(); + + Connector connector = new SelectChannelConnector(); + connector.setPort(8080); + server.setConnectors(new Connector[] { connector }); + + WebAppContext webapp = new WebAppContext("./root", "/example"); + server.addHandler(webapp); + + server.start(); + server.join(); + } +} diff --git a/struts2/example/src/main/java/com/google/inject/struts2/example/Count.java b/struts2/example/src/main/java/com/google/inject/struts2/example/Count.java deleted file mode 100644 index 4fca1516..00000000 --- a/struts2/example/src/main/java/com/google/inject/struts2/example/Count.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (C) 2006 Google Inc. - * - * 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 com.google.inject.struts2.example; - -import com.google.inject.Inject; - -import static com.opensymphony.xwork2.Action.SUCCESS; - -public class Count { - - final Counter counter; - - @Inject - public Count(Counter counter) { - this.counter = counter; - } - - public String execute() { - return SUCCESS; - } - - public int getCount() { - return counter.increment(); - } -} diff --git a/struts2/example/src/main/java/com/google/inject/struts2/example/Counter.java b/struts2/example/src/main/java/com/google/inject/struts2/example/Counter.java deleted file mode 100644 index c375b981..00000000 --- a/struts2/example/src/main/java/com/google/inject/struts2/example/Counter.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright (C) 2006 Google Inc. - * - * 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 com.google.inject.struts2.example; - -import com.google.inject.servlet.SessionScoped; - -/** - * Session-scoped counter. - */ -@SessionScoped -public class Counter { - - int count = 0; - - /** Increments the count and returns the new value. */ - public synchronized int increment() { - return count++; - } -} diff --git a/struts2/example/src/main/java/com/google/inject/struts2/example/ExampleModule.java b/struts2/example/src/main/java/com/google/inject/struts2/example/ExampleModule.java deleted file mode 100644 index b8a04f7e..00000000 --- a/struts2/example/src/main/java/com/google/inject/struts2/example/ExampleModule.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (C) 2006 Google Inc. - * - * 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 com.google.inject.struts2.example; - -import com.google.inject.AbstractModule; - -/** - * Example application module. - * - * @author crazybob@google.com (Bob Lee) - */ -public class ExampleModule extends AbstractModule { - - protected void configure() { - } -} diff --git a/struts2/example/src/main/java/com/google/inject/struts2/example/Main.java b/struts2/example/src/main/java/com/google/inject/struts2/example/Main.java deleted file mode 100644 index 60fd0bec..00000000 --- a/struts2/example/src/main/java/com/google/inject/struts2/example/Main.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (C) 2006 Google Inc. - * - * 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 com.google.inject.struts2.example; - -import org.mortbay.jetty.Connector; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.webapp.WebAppContext; -import org.mortbay.jetty.nio.SelectChannelConnector; - -/** - * Starts the example web server on port 8080. Run from "./struts2/example". - */ -public class Main { - - public static void main(String[] args) throws Exception { - Server server = new Server(); - - Connector connector = new SelectChannelConnector(); - connector.setPort(8080); - server.setConnectors(new Connector[] { connector }); - - WebAppContext webapp = new WebAppContext("./root", "/example"); - server.addHandler(webapp); - - server.start(); - server.join(); - } -} diff --git a/struts2/plugin/build.properties b/struts2/plugin/build.properties index 794228ac..31ee68de 100644 --- a/struts2/plugin/build.properties +++ b/struts2/plugin/build.properties @@ -1,4 +1,4 @@ version=1.0rc2 lib.dir=../lib -src.dir=src/main/java +src.dir=src build.dir=build diff --git a/struts2/plugin/src/com/google/inject/struts2/GuiceObjectFactory.java b/struts2/plugin/src/com/google/inject/struts2/GuiceObjectFactory.java new file mode 100644 index 00000000..588222c3 --- /dev/null +++ b/struts2/plugin/src/com/google/inject/struts2/GuiceObjectFactory.java @@ -0,0 +1,176 @@ +/** + * Copyright (C) 2006 Google Inc. + * + * 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 com.google.inject.struts2; + +import com.google.inject.Container; +import com.google.inject.Module; +import com.google.inject.Guice; +import com.google.inject.AbstractModule; +import com.google.inject.servlet.ServletModule; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ObjectFactory; +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.config.entities.InterceptorConfig; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.interceptor.Interceptor; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +public class GuiceObjectFactory extends ObjectFactory { + + static final Logger logger = + Logger.getLogger(GuiceObjectFactory.class.getName()); + + Module module; + Container container; + boolean developmentMode = false; + + @Override + public boolean isNoArgConstructorRequired() { + return false; + } + + @Inject(value = "guice.module", required = false) + void setModule(String moduleClassName) { + try { + // Instantiate user's module. + @SuppressWarnings({"unchecked"}) + Class moduleClass = + (Class) Class.forName(moduleClassName); + this.module = moduleClass.newInstance(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Inject(value = "struts.devMode", required = false) + void setDevelopmentMode(String developmentMode) { + this.developmentMode = developmentMode.trim().equals("true"); + } + + Set> boundClasses = new HashSet>(); + + public Class getClassInstance(String name) throws ClassNotFoundException { + Class clazz = super.getClassInstance(name); + + synchronized (this) { + if (container == null) { + // We can only bind each class once. + if (!boundClasses.contains(clazz)) { + try { + // Calling these methods now helps us detect ClassNotFoundErrors + // early. + clazz.getDeclaredFields(); + clazz.getDeclaredMethods(); + + boundClasses.add(clazz); + } catch (Throwable t) { + // Struts should still work even though some classes aren't in the + // classpath. It appears we always get the exception here when + // this is the case. + return clazz; + } + } + } + } + + return clazz; + } + + public Object buildBean(Class clazz, Map extraContext) { + synchronized (this) { + if (container == null) { + try { + logger.info("Creating container..."); + this.container = Guice.createContainer(new AbstractModule() { + protected void configure() { + install(new ServletModule()); + + // Tell the container about all the action classes, etc., so it + // can validate them at startup. + for (Class boundClass : boundClasses) { + bind(boundClass); + } + } + }); + } catch (Throwable t) { + t.printStackTrace(); + System.exit(1); + } + logger.info("Container created successfully."); + } + } + + return container.getProvider(clazz).get(); + } + + public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, + Map interceptorRefParams) throws ConfigurationException { + try { + getClassInstance(interceptorConfig.getClassName()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + // Defer the creation of interceptors so that we don't have to create the + // container until we've bound all the actions. This enables us to + // validate all the dependencies at once. + return new LazyLoadedInterceptor(interceptorConfig, interceptorRefParams); + } + + Interceptor superBuildInterceptor(InterceptorConfig interceptorConfig, + Map interceptorRefParams) throws ConfigurationException { + return super.buildInterceptor(interceptorConfig, interceptorRefParams); + } + + class LazyLoadedInterceptor implements Interceptor { + + final InterceptorConfig config; + final Map params; + + LazyLoadedInterceptor(InterceptorConfig config, Map params) { + this.config = config; + this.params = params; + } + + Interceptor delegate = null; + + synchronized Interceptor getDelegate() { + if (delegate == null) { + delegate = superBuildInterceptor(config, params); + delegate.init(); + } + return delegate; + } + + public void destroy() { + getDelegate().destroy(); + } + + public void init() { + throw new AssertionError(); + } + + public String intercept(ActionInvocation invocation) throws Exception { + return getDelegate().intercept(invocation); + } + } +} diff --git a/struts2/plugin/src/main/java/com/google/inject/struts2/GuiceObjectFactory.java b/struts2/plugin/src/main/java/com/google/inject/struts2/GuiceObjectFactory.java deleted file mode 100644 index 588222c3..00000000 --- a/struts2/plugin/src/main/java/com/google/inject/struts2/GuiceObjectFactory.java +++ /dev/null @@ -1,176 +0,0 @@ -/** - * Copyright (C) 2006 Google Inc. - * - * 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 com.google.inject.struts2; - -import com.google.inject.Container; -import com.google.inject.Module; -import com.google.inject.Guice; -import com.google.inject.AbstractModule; -import com.google.inject.servlet.ServletModule; - -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.ObjectFactory; -import com.opensymphony.xwork2.config.ConfigurationException; -import com.opensymphony.xwork2.config.entities.InterceptorConfig; -import com.opensymphony.xwork2.inject.Inject; -import com.opensymphony.xwork2.interceptor.Interceptor; - -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.logging.Logger; - -public class GuiceObjectFactory extends ObjectFactory { - - static final Logger logger = - Logger.getLogger(GuiceObjectFactory.class.getName()); - - Module module; - Container container; - boolean developmentMode = false; - - @Override - public boolean isNoArgConstructorRequired() { - return false; - } - - @Inject(value = "guice.module", required = false) - void setModule(String moduleClassName) { - try { - // Instantiate user's module. - @SuppressWarnings({"unchecked"}) - Class moduleClass = - (Class) Class.forName(moduleClassName); - this.module = moduleClass.newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Inject(value = "struts.devMode", required = false) - void setDevelopmentMode(String developmentMode) { - this.developmentMode = developmentMode.trim().equals("true"); - } - - Set> boundClasses = new HashSet>(); - - public Class getClassInstance(String name) throws ClassNotFoundException { - Class clazz = super.getClassInstance(name); - - synchronized (this) { - if (container == null) { - // We can only bind each class once. - if (!boundClasses.contains(clazz)) { - try { - // Calling these methods now helps us detect ClassNotFoundErrors - // early. - clazz.getDeclaredFields(); - clazz.getDeclaredMethods(); - - boundClasses.add(clazz); - } catch (Throwable t) { - // Struts should still work even though some classes aren't in the - // classpath. It appears we always get the exception here when - // this is the case. - return clazz; - } - } - } - } - - return clazz; - } - - public Object buildBean(Class clazz, Map extraContext) { - synchronized (this) { - if (container == null) { - try { - logger.info("Creating container..."); - this.container = Guice.createContainer(new AbstractModule() { - protected void configure() { - install(new ServletModule()); - - // Tell the container about all the action classes, etc., so it - // can validate them at startup. - for (Class boundClass : boundClasses) { - bind(boundClass); - } - } - }); - } catch (Throwable t) { - t.printStackTrace(); - System.exit(1); - } - logger.info("Container created successfully."); - } - } - - return container.getProvider(clazz).get(); - } - - public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, - Map interceptorRefParams) throws ConfigurationException { - try { - getClassInstance(interceptorConfig.getClassName()); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - - // Defer the creation of interceptors so that we don't have to create the - // container until we've bound all the actions. This enables us to - // validate all the dependencies at once. - return new LazyLoadedInterceptor(interceptorConfig, interceptorRefParams); - } - - Interceptor superBuildInterceptor(InterceptorConfig interceptorConfig, - Map interceptorRefParams) throws ConfigurationException { - return super.buildInterceptor(interceptorConfig, interceptorRefParams); - } - - class LazyLoadedInterceptor implements Interceptor { - - final InterceptorConfig config; - final Map params; - - LazyLoadedInterceptor(InterceptorConfig config, Map params) { - this.config = config; - this.params = params; - } - - Interceptor delegate = null; - - synchronized Interceptor getDelegate() { - if (delegate == null) { - delegate = superBuildInterceptor(config, params); - delegate.init(); - } - return delegate; - } - - public void destroy() { - getDelegate().destroy(); - } - - public void init() { - throw new AssertionError(); - } - - public String intercept(ActionInvocation invocation) throws Exception { - return getDelegate().intercept(invocation); - } - } -} diff --git a/struts2/plugin/src/main/java/struts-plugin.xml b/struts2/plugin/src/main/java/struts-plugin.xml deleted file mode 100644 index 6a317568..00000000 --- a/struts2/plugin/src/main/java/struts-plugin.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - diff --git a/struts2/plugin/src/struts-plugin.xml b/struts2/plugin/src/struts-plugin.xml new file mode 100644 index 00000000..6a317568 --- /dev/null +++ b/struts2/plugin/src/struts-plugin.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + -- cgit v1.2.3