Java Doc for Active.java in  » Web-Server » JicarillaHTTP » org » jicarilla » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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 » Web Server » JicarillaHTTP » org.jicarilla.lang 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


org.jicarilla.lang.Active

All known Subclasses:   org.jicarilla.lang.AbstractActive,  org.jicarilla.webserver.PureJavaMain,
Active
public interface Active (Code)

An interface implemented by objects which need to do some kind of post-construction initialization before they can be used (like the creation of worker threads).

The Active contract

For any component which implements this interface, the Active.initialize() method must be the first method called or field accessed after an instance is constructed, and it must be called only once It is usually safest to call initialize() immediately after construction to make sure of that. Note that it is okay to create an instance of a component which implements active and never do anything with it, not even calling initialize() or Active.dispose() .

For any component which implements this interface, the dispose() method must be called exactly once. There is one exception to this rule: if initialize() is never called, dispose() should never be called, either. After it has been called, no more methods should be called on the instance, nor should any fields be accessed. There is an exception to this rule as well: the garbage collector may call a finalize() method if it exists, which in turn may access fields and call other methods.

State definitions

With passive components, we just have safe (the component can be safely used) and unsafe (the component cannot be safely used) state. With active components, we usually introduce some more terminology to describe component state:

An object whose constructor has returned has been created. While the initialize() method is running the component is initializing. Once the initialize() method has returned, the object is initialized and active. It will remain active until the dispose() method is called. While the dispose() method is running, the component is disposing or shutting down. Once the dispose() method has returned, the component has shut down and it has been disposed.

The contract in readable English...

After you create an instance of an Active component, call its initialize() method. Then use the component as you would use any other. When you are finished using it, call the dispose() method.

    some tips:
  • call initialize() immediately after you construct an instance.
  • To make sure an instance is only ever initialize()d and dispose()d once, keep the responsibility for calling these methods with the same code that is responsible for creating the instance.
  • Remove all references to an instance immediately after you have called dispose() (to make sure you don't accidentally call a method on it again later, and so that the component may be reclaimed for garbage collection).

Some notes on lifecycle management

The Active interface is the only lifecycle interface to make it into the Jicarilla Framework. You should try to avoid its use as much as possible, as components that depend on having initialize() and dispose() require more work by the client programmer.

One good reason to use the Active interface is when you need to use threads, since those should not be created inside a constructor (see Well Behaved Citizens for more information).

Also, while the contract surrounding Active should be crystal-clear to everyone and it should always.

For example of the Active interface in use, take a look at any of the jicarilla components. You will note its use is minimal: most components defer their thread management to helper components like thread pools, freeing them from the need to be Active.

The inversion of active is passive. Try to make most of your code passive, controlled by isolated bits of active code.

Enforcing the Active contract

There's many different things you can do to make sure that client code doesn't fail, or at least fails gracefully, even if it doesn't honor the Active contract correctly:

  • extend from the org.jicarilla.lang.AbstractActiveAbstractActive interface. It implements basic 'wrapper' methods around initialize() and dispose() that shield the client class from clients that call these methods in an inappropriate order, or more than once.
  • implement lazy (re)initialization. To do this, you make all your fields private, then add checks at the top of every public method (and every protected method if you're really playing it safe) that will initialize() a component if it hasn't been initialized yet, and maybe even reset, then re-initialize a component if it has been disposed of already. The AbstractActive class implements such a policy inside the org.jicarilla.lang.AbstractActive.lazyInitializationlazyInitialization() method.
  • implement state checking. To do this, add checks at the top of every public method that will throw an exception if the component is not in an active state. The AbstractActive class implements such a policy inside org.jicarilla.lang.AbstractActive.checkActivecheckActive() method.
  • Use a container. If you run your components inside an IoC container that is aware of the Active interface, such as Jicarilla-Container, the container can take care of calling the initialize() and dispose() methods for you automatically. It can even shield your components from malicious clients by implementing a proxy (or a security policy) that will prevent them from calling the initialize() or dispose() methods at all!

Before you go overboard with these measures, consider the alternative: trust. If you trust users of your components to be able to always remember and follow the Active contract, don't bother with all the checks (they clutter up the code and add overhead).

Active components and thread safety

Active components need all the usual synchronization and checking to be safe in a multithreaded enviroment. In particular, be extra careful when using lazy initialization or state checking, since you open up the possibility of two concurrently called methods deciding to do lazy initialization at the same time. Note that the relevant methods for these two policies inside AbstractActive are all synchronized to prevent this kind of problem.


author:
   Leo Simons
version:
   $Id: Active.java,v 1.1 2004/03/23 13:37:56 lsimons Exp $




Method Summary
 voiddispose()
     Shut down this object, telling it to clean up after itself (usually stopping and disposal of worker threads).
 voidinitialize()
     Initialize this object so that it is ready to be used.



Method Detail
dispose
void dispose() throws Throwable(Code)
Shut down this object, telling it to clean up after itself (usually stopping and disposal of worker threads). If an exception is thrown from this
throws:
  Throwable - if some problem occurs during disposition.



initialize
void initialize() throws Throwable(Code)
Initialize this object so that it is ready to be used. If an exception is thrown from this method, the instance should be assumed to be in an unsafe state, Active.dispose() should be immediately called, and then all references to the instance should be disposed of. Needless to say, throwing exceptions from this method should be a rare event.
throws:
  Throwable - if some problem occurs during initialization.



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.