View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.domain.dao;
17  
18  import org.springframework.util.Assert;
19  
20  
21  
22  /***
23   * Convenience methods that support initialization of lazily loaded collections
24   * and associations using DAOs and other objects that implement
25   * {@link org.acegisecurity.domain.dao.InitializationCapable}.
26   *
27   * @author Ben Alex
28   * @version $Id: InitializationUtils.java,v 1.3 2005/11/17 00:55:47 benalex Exp $
29   */
30  public class InitializationUtils {
31      //~ Methods ================================================================
32  
33      /***
34       * Initializes the passed entity using the passed
35       * DAO or services layer <code>Object</code> (provided that the passed
36       * <code>Object</code> implements <code>InitializationCapable</code>).
37       *
38       * @param daoOrServices the potential source for
39       *        <code>InitializationCapable</code> services (never <code>null</code>)
40       * @param entity to evict (can be <code>null</code>)
41       */
42      public static void initializeIfRequired(Object daoOrServices,
43          Object entity) {
44  		Assert.notNull(daoOrServices);
45  		if (daoOrServices instanceof InitializationCapable) {
46  			((InitializationCapable) daoOrServices).initialize(entity);
47  		}
48      }
49  	
50  	/***
51  	 * Indicates whether the passed entity has been initialized, by delegating
52  	 * to the passed daoOrServices (provided that the passed daoOrServices
53  	 * implements <code>InitializationCapable</code>.
54  	 * 
55  	 * @param entity to determine whether initialized or not
56  	 * @return <code>true</code> if initialized, <code>false</code> if it is
57  	 * 			uninitialized or the passed daoOrServices does not provide
58  	 * 			initialization querying support
59  	 */
60  	public static boolean isInitialized(Object daoOrServices, Object entity) {
61  		Assert.notNull(daoOrServices);
62  		if (daoOrServices instanceof InitializationCapable) {
63  			return ((InitializationCapable) daoOrServices).isInitialized(entity);
64  		}
65  		return false;
66  	}
67  }