|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ContainerUtil.java | 100% | 100% | 100% | 100% |
|
1 |
/*
|
|
2 |
* Copyright (C) The JContainer Group. All rights reserved.
|
|
3 |
*
|
|
4 |
* This software is published under the terms of the JContainer
|
|
5 |
* Software License version 1.1, a copy of which has been included
|
|
6 |
* with this distribution in the LICENSE.txt file.
|
|
7 |
*/
|
|
8 |
package org.jcontainer.dna.impl;
|
|
9 |
|
|
10 |
import org.jcontainer.dna.Active;
|
|
11 |
import org.jcontainer.dna.Composable;
|
|
12 |
import org.jcontainer.dna.Configurable;
|
|
13 |
import org.jcontainer.dna.Configuration;
|
|
14 |
import org.jcontainer.dna.ConfigurationException;
|
|
15 |
import org.jcontainer.dna.LogEnabled;
|
|
16 |
import org.jcontainer.dna.Logger;
|
|
17 |
import org.jcontainer.dna.MissingResourceException;
|
|
18 |
import org.jcontainer.dna.ParameterException;
|
|
19 |
import org.jcontainer.dna.Parameterizable;
|
|
20 |
import org.jcontainer.dna.Parameters;
|
|
21 |
import org.jcontainer.dna.ResourceLocator;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Utility class to make it easier to process a object
|
|
25 |
* through its lifecycle stages.
|
|
26 |
*
|
|
27 |
* @version $Revision: 1.4 $ $Date: 2003/09/23 08:10:14 $
|
|
28 |
*/
|
|
29 |
public class ContainerUtil |
|
30 |
{ |
|
31 |
/**
|
|
32 |
* Supply specified object with Logger if it implements the
|
|
33 |
* LogEnabled interface.
|
|
34 |
*
|
|
35 |
* @param object the object to process
|
|
36 |
* @param logger the logger. If null then the specified object must
|
|
37 |
* not implement LogEnabled.
|
|
38 |
* @throws IllegalArgumentException if the object is LogEnabled and
|
|
39 |
* Logger is null
|
|
40 |
*/
|
|
41 | 7 |
public static void enableLogging( final Object object, |
42 |
final Logger logger ) |
|
43 |
{ |
|
44 | 7 |
if( object instanceof LogEnabled ) |
45 |
{ |
|
46 | 4 |
if( null == logger ) |
47 |
{ |
|
48 | 2 |
final String message = "Null logger.";
|
49 | 2 |
throw new IllegalArgumentException( message ); |
50 |
} |
|
51 | 2 |
( (LogEnabled)object ).enableLogging( logger ); |
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
/**
|
|
56 |
* Supply specified object with ResourceLocator if it implements the
|
|
57 |
* Composable interface.
|
|
58 |
*
|
|
59 |
* @param object the object to process
|
|
60 |
* @param locator the ResourceLocator. If null then the specified
|
|
61 |
* object must not implement Composable.
|
|
62 |
* @throws IllegalArgumentException if the object is Composable
|
|
63 |
* and locator is null
|
|
64 |
* @throws MissingResourceException if processing lifecycle stage on
|
|
65 |
* object throws exception
|
|
66 |
*/
|
|
67 | 6 |
public static void compose( final Object object, |
68 |
final ResourceLocator locator ) |
|
69 |
throws MissingResourceException
|
|
70 |
{ |
|
71 | 6 |
if( object instanceof Composable ) |
72 |
{ |
|
73 | 4 |
if( null == locator ) |
74 |
{ |
|
75 | 2 |
final String message = "Null locator.";
|
76 | 2 |
throw new IllegalArgumentException( message ); |
77 |
} |
|
78 | 2 |
( (Composable)object ).compose( locator ); |
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* Supply specified object with Configuration if it implements the
|
|
84 |
* Configurable interface.
|
|
85 |
*
|
|
86 |
* @param object the object to process
|
|
87 |
* @param configuration the Configuration. If null then the specified
|
|
88 |
* object must not implement Configurable.
|
|
89 |
* @throws IllegalArgumentException if the object is Configurable
|
|
90 |
* and configuration is null
|
|
91 |
* @throws ConfigurationException if processing lifecycle stage on
|
|
92 |
* object throws exception
|
|
93 |
*/
|
|
94 | 6 |
public static void configure( final Object object, |
95 |
final Configuration configuration ) |
|
96 |
throws ConfigurationException
|
|
97 |
{ |
|
98 | 6 |
if( object instanceof Configurable ) |
99 |
{ |
|
100 | 4 |
if( null == configuration ) |
101 |
{ |
|
102 | 2 |
final String message = "Null configuration.";
|
103 | 2 |
throw new IllegalArgumentException( message ); |
104 |
} |
|
105 | 2 |
( (Configurable)object ).configure( configuration ); |
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
/**
|
|
110 |
* Supply specified object with Parameters if it implements the
|
|
111 |
* Parameterizable interface.
|
|
112 |
*
|
|
113 |
* @param object the object to process
|
|
114 |
* @param parameters the Parameters. If null then the specified
|
|
115 |
* object must not implement Parameterizable.
|
|
116 |
* @throws IllegalArgumentException if the object is Parameterizable
|
|
117 |
* and parameters is null
|
|
118 |
* @throws ParameterException if processing lifecycle stage on
|
|
119 |
* object throws exception
|
|
120 |
*/
|
|
121 | 6 |
public static void parameterize( final Object object, |
122 |
final Parameters parameters ) |
|
123 |
throws ParameterException
|
|
124 |
{ |
|
125 | 6 |
if( object instanceof Parameterizable ) |
126 |
{ |
|
127 | 4 |
if( null == parameters ) |
128 |
{ |
|
129 | 2 |
final String message = "Null parameters.";
|
130 | 2 |
throw new IllegalArgumentException( message ); |
131 |
} |
|
132 | 2 |
( (Parameterizable)object ).parameterize( parameters ); |
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
/**
|
|
137 |
* Initialize specified object if it implements the
|
|
138 |
* Active interface.
|
|
139 |
*
|
|
140 |
* @param object the object to process
|
|
141 |
* @throws Exception if processing lifecycle stage on
|
|
142 |
* object throws exception
|
|
143 |
*/
|
|
144 | 4 |
public static void initialize( final Object object ) |
145 |
throws Exception
|
|
146 |
{ |
|
147 | 4 |
if( object instanceof Active ) |
148 |
{ |
|
149 | 2 |
( (Active)object ).initialize(); |
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
/**
|
|
154 |
* Dispose specified object if it implements the
|
|
155 |
* Active interface.
|
|
156 |
*
|
|
157 |
* @param object the object to process
|
|
158 |
* @throws Exception if processing lifecycle stage on
|
|
159 |
* object throws exception
|
|
160 |
*/
|
|
161 | 4 |
public static void dispose( final Object object ) |
162 |
throws Exception
|
|
163 |
{ |
|
164 | 4 |
if( object instanceof Active ) |
165 |
{ |
|
166 | 2 |
( (Active)object ).dispose(); |
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
|
|