Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LifecycleUtils |
|
| 3.3333333333333335;3.333 |
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 | 0 | public abstract class LifecycleUtils { |
35 | ||
36 | 1 | private static final Logger log = LoggerFactory.getLogger(LifecycleUtils.class); |
37 | ||
38 | public static void init(Object o) throws ShiroException { | |
39 | 95 | if (o instanceof Initializable) { |
40 | 22 | init((Initializable) o); |
41 | } | |
42 | 95 | } |
43 | ||
44 | public static void init(Initializable initializable) throws ShiroException { | |
45 | 22 | initializable.init(); |
46 | 22 | } |
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 | 42 | if (c == null || c.isEmpty()) { |
58 | 0 | return; |
59 | } | |
60 | 42 | for (Object o : c) { |
61 | 95 | init(o); |
62 | 95 | } |
63 | 42 | } |
64 | ||
65 | public static void destroy(Object o) { | |
66 | 110 | if (o instanceof Destroyable) { |
67 | 21 | destroy((Destroyable) o); |
68 | 89 | } else if (o instanceof Collection) { |
69 | 0 | destroy((Collection)o); |
70 | } | |
71 | 110 | } |
72 | ||
73 | public static void destroy(Destroyable d) { | |
74 | 21 | if (d != null) { |
75 | try { | |
76 | 21 | d.destroy(); |
77 | 0 | } catch (Throwable t) { |
78 | 0 | if (log.isDebugEnabled()) { |
79 | 0 | String msg = "Unable to cleanly destroy instance [" + d + "] of type [" + d.getClass().getName() + "]."; |
80 | 0 | log.debug(msg, t); |
81 | } | |
82 | 21 | } |
83 | } | |
84 | 21 | } |
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 | 19 | if (c == null || c.isEmpty()) { |
95 | 1 | return; |
96 | } | |
97 | ||
98 | 18 | for (Object o : c) { |
99 | 18 | destroy(o); |
100 | 18 | } |
101 | 18 | } |
102 | } |