View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.util;
20  
21  import org.apache.shiro.ShiroException;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.util.Collection;
26  
27  
28  /**
29   * Utility class to help call {@link org.apache.shiro.util.Initializable#init() Initializable.init()} and
30   * {@link org.apache.shiro.util.Destroyable#destroy() Destroyable.destroy()} methods cleanly on any object.
31   *
32   * @since 0.2
33   */
34  public abstract class LifecycleUtils {
35  
36      private static final Logger log = LoggerFactory.getLogger(LifecycleUtils.class);
37  
38      public static void init(Object o) throws ShiroException {
39          if (o instanceof Initializable) {
40              init((Initializable) o);
41          }
42      }
43  
44      public static void init(Initializable initializable) throws ShiroException {
45          initializable.init();
46      }
47  
48      /**
49       * Calls {@link #init(Object) init} for each object in the collection.  If the collection is {@code null} or empty,
50       * this method returns quietly.
51       *
52       * @param c the collection containing objects to {@link #init init}.
53       * @throws ShiroException if unable to initialize one or more instances.
54       * @since 0.9
55       */
56      public static void init(Collection c) throws ShiroException {
57          if (c == null || c.isEmpty()) {
58              return;
59          }
60          for (Object o : c) {
61              init(o);
62          }
63      }
64  
65      public static void destroy(Object o) {
66          if (o instanceof Destroyable) {
67              destroy((Destroyable) o);
68          } else if (o instanceof Collection) {
69              destroy((Collection)o);
70          }
71      }
72  
73      public static void destroy(Destroyable d) {
74          if (d != null) {
75              try {
76                  d.destroy();
77              } catch (Throwable t) {
78                  if (log.isDebugEnabled()) {
79                      String msg = "Unable to cleanly destroy instance [" + d + "] of type [" + d.getClass().getName() + "].";
80                      log.debug(msg, t);
81                  }
82              }
83          }
84      }
85  
86      /**
87       * Calls {@link #destroy(Object) destroy} for each object in the collection.
88       * If the collection is {@code null} or empty, this method returns quietly.
89       *
90       * @param c the collection of objects to destroy.
91       * @since 0.9
92       */
93      public static void destroy(Collection c) {
94          if (c == null || c.isEmpty()) {
95              return;
96          }
97  
98          for (Object o : c) {
99              destroy(o);
100         }
101     }
102 }