nanocontainer

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Inversion of Control » nanocontainer 
NanoContainer(Inversion of Control)
License:
URL:http://www.nanocontainer.org/
Description:
Package NameComment
org.nanocontainer
org.nanocontainer.aop

Lets you configure aspects on components in a {@link org.picocontainer.PicoContainer}. Two kinds of advice are supported, interceptors and mixins. An interceptor is invoked around method calls. Interceptors implement the org.aopalliance.interceptor.MethodInterceptor interface. Mixins allow you to add new methods and behavior to an existing object. You can think of them as a way of dynamically providing multiple inheritance in java. Mixins can be any kind of object; they are not required to extend or implement anything. You can configure aspects in Java, or using groovy. Advice can be configured for all components in a container, using pointcuts, or advice can be applied to just one component. Advice objects may themselves have dependencies on other components, supplied by the container. Currently, dynaop is used to apply the aspects. Other backends may be supported in the future.

Configuring Aspects

For the following examples, we'll use the following logging interceptor:
    public class LoggingInterceptor implements org.aopalliance.intercept.MethodInterceptor {
        public Object invoke(MethodInvocation invocation) throws Throwable {
            System.out.println("start");
            Object result = invocation.proceed();
            System.out.println("end");
            return result;
        }
    }
We'll also assume that we have a Dao interface, with an implementation class DaoImpl.

Java Configuration

The primary interface for configuring aspects in Java is {@link org.nanocontainer.aop.AspectablePicoContainer}. To create one you need a {@link org.nanocontainer.aop.AspectablePicoContainerFactory}. The following example applies our logging interceptor to all methods of all instances of Dao in the container:
    AspectablePicoContainerFactory containerFactory = new DynaopAspectablePicoContainerFactory();
    AspectablePicoContainer pico = containerFactory.createContainer();
    PointcutsFactory cuts = pico.getPointcutsFactory();
    
    LoggingInterceptor logger = new LoggingInterceptor();
    pico.registerComponentImplementation(Dao.class, DaoImpl.class);
    pico.registerInterceptor(cuts.instancesOf(Dao.class), cuts.allMethods(), logger);

    Dao dao = (Dao) pico.getComponentInstance(Dao.class);
    dao.aMethod(); // will print 'startend'

If we wanted to apply our interceptor to just one component, we could do the following:

    pico.registerComponentImplementation("myDao", DaoImpl.class);
    pico.registerInterceptor(cuts.component("myDao"), cuts.allMethods(), logger);
Note that components and aspects can be registered in any order. An aspect can be registered before the component it applies to. In the example above, we could have reversed the two lines and registered the interceptor before the component.

Container Supplied Advice

Advice objects may themselves be components in the PicoContainer, with dependencies on other components. Let's revise our LoggingInterceptor to have a dependency on a log object of some sort. For simplicity, we'll just use a StringBuffer as our logger (in real life it might be a log4j or a commons logging object of some sort):
    public class LoggingInterceptor {
        private final StringBuffer log;
        
        public LoggingInterceptor(StringBuffer log) {
            this.log = log;
        }
        
        public Object invoke(MethodInvocation invocation) throws Throwable {
            log.append("start");
            Object result = invocation.proceed();
            log.append("end");
            return result;
        }
    }
With this in place, we could then register our LoggingInterceptor and apply it to a component as follows:
    // register dependency of LoggingInterceptor:
    pico.registerComponentInstance("log", StringBuffer.log); 
    
    pico.registerComponentImplementation("logger", LoggingInterceptor.class);
    pico.registerComponentImplementation("myDao", DaoImpl.class);

    // you can specify the component key of the advice object:
    pico.registerInterceptor(cuts.component("myDao"), cuts.allMethods(), "logger");
Pico will supply the StringBuffer "log" component to the constructor of the LoggingInterceptor.

Mixins are a special case. While you can register a mixin as an explicit component in the container, you usually don't want to. Applying the same mixin instance as advice to multiple components would in effect mean that multiple components would share the same base class object - sharing the same instances variables, etc. This is usually not a good thing. However, you may specify a mixin class that has dependencies on components in the container. The container will instantiate a new mixin object for each component that it applies the mixin to, but will satisfy any dependencies the mixin has from components in the container, using constructor injection.

Pointcuts

Notice the use of the {@link org.nanocontainer.aop.PointcutsFactory}, cuts, in the first example. This interface provides methods for producing pointcuts that match one class or method, or that match the class, method or component name against a regular expressionm, pointcuts that pick all instances of a given class, or all classes in a given package, etc. You are not limited to the pointcuts produced by PointcutsFactory however. Any pointcut that implements one of the interfaces {@link org.nanocontainer.aop.ComponentPointcut}, {@link org.nanocontainer.aop.ClassPointcut}, {@link org.nanocontainer.aop.MethodPointcut} will work. Class pointcuts match against the component's class. Component pointcuts match against the component key. Method pointcuts match against the method being invoked. Method pointcuts only apply to interceptor advice; they are not used for mixins.

Groovy Configuration

To configure aspects using groovy, use {@link org.nanocontainer.aop.script.groovy.DynaopNanoContainerBuilder}.

Limitations

Components that have aspects applied to them must implement an interface. Advice is not applied to components registered via registerComponentInstance, or for components supplied via a custom ComponentAdapter.
org.nanocontainer.aop.defaults Provides partial or default implementations of the interfaces in org.nanocontainer.aop, and common classes used by different sub-packages under org.nanocontainer.aop.
org.nanocontainer.aop.dynaop Implements org.nanocontainer.aop using dynaop.
org.nanocontainer.booter
org.nanocontainer.deployer
org.nanocontainer.integrationkit
org.nanocontainer.reflection
org.nanocontainer.script
org.nanocontainer.script.bsh
org.nanocontainer.script.groovy

This package contains Groovy scripting support for NanoContainer assembly.

More information about the groovy scripting language may be found at http://groovy.codehaus.org/

org.nanocontainer.script.groovy.buildernodes Untitled Document

This package contains default builder nodes for NanoContainer Groovy script assembly. Each class represents one type of node that will be handled.

The following diagram shows the typical relations in the builder nodes.

Builder UML

In other words:

  • GroovyNodeBuilder contains a Map of CustomGroovyNodes that are keyed by CustomGroovyNode.getNodeName(). It uses this map to locate handlers whenever a groovy script node is encountered.
  • For each GroovyScriptNode encountered, an attempt to match on a handler is performed. If one is found, then it calls CustomGroovyNode.createNewNode().
  • Most (if not all) of the groovy nodes derive from the AbstractCustomBuilderNode that provides services such as attribute placeholding, and default implementation of getNodeName().

 

org.nanocontainer.script.jruby
org.nanocontainer.script.jython
org.nanocontainer.script.rhino
org.nanocontainer.script.xml
org.nanocontainer.script.xml.issues
org.nanocontainer.testmodel
org.nanocontainer.webcontainer
org.nanocontainer.webcontainer.groovy
org.nanocontainer.webcontainer.groovy.adapters
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.