001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.util;
020    
021    import org.apache.shiro.ShiroException;
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
024    
025    import java.util.Collection;
026    
027    
028    /**
029     * Utility class to help call {@link org.apache.shiro.util.Initializable#init() Initializable.init()} and
030     * {@link org.apache.shiro.util.Destroyable#destroy() Destroyable.destroy()} methods cleanly on any object.
031     *
032     * @author Les Hazlewood
033     * @since 0.2
034     */
035    public abstract class LifecycleUtils {
036    
037        private static final Logger log = LoggerFactory.getLogger(LifecycleUtils.class);
038    
039        public static void init(Object o) throws ShiroException {
040            if (o instanceof Initializable) {
041                init((Initializable) o);
042            }
043        }
044    
045        public static void init(Initializable initializable) throws ShiroException {
046            initializable.init();
047        }
048    
049        /**
050         * Calls {@link #init(Object) init} for each object in the collection.  If the collection is {@code null} or empty,
051         * this method returns quietly.
052         *
053         * @param c the collection containing objects to {@link #init init}.
054         * @throws ShiroException if unable to initialize one or more instances.
055         * @since 0.9
056         */
057        public static void init(Collection c) throws ShiroException {
058            if (c == null || c.isEmpty()) {
059                return;
060            }
061            for (Object o : c) {
062                init(o);
063            }
064        }
065    
066        public static void destroy(Object o) {
067            if (o instanceof Destroyable) {
068                destroy((Destroyable) o);
069            }
070        }
071    
072        public static void destroy(Destroyable d) {
073            if (d != null) {
074                try {
075                    d.destroy();
076                } catch (Throwable t) {
077                    if (log.isDebugEnabled()) {
078                        String msg = "Unable to cleanly destroy instance [" + d + "] of type [" + d.getClass().getName() + "].";
079                        log.debug(msg, t);
080                    }
081                }
082            }
083        }
084    
085        /**
086         * Calls {@link #destroy(Object) destroy} for each object in the collection.
087         * If the collection is {@code null} or empty, this method returns quietly.
088         *
089         * @param c the collection of objects to destroy.
090         * @since 0.9
091         */
092        public static void destroy(Collection c) {
093            if (c == null || c.isEmpty()) {
094                return;
095            }
096    
097            for (Object o : c) {
098                destroy(o);
099            }
100        }
101    }