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 */
019package org.apache.shiro.util;
020
021import org.apache.shiro.ShiroException;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import 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 * @since 0.2
033 */
034public abstract class LifecycleUtils {
035
036    private static final Logger log = LoggerFactory.getLogger(LifecycleUtils.class);
037
038    public static void init(Object o) throws ShiroException {
039        if (o instanceof Initializable) {
040            init((Initializable) o);
041        }
042    }
043
044    public static void init(Initializable initializable) throws ShiroException {
045        initializable.init();
046    }
047
048    /**
049     * Calls {@link #init(Object) init} for each object in the collection.  If the collection is {@code null} or empty,
050     * this method returns quietly.
051     *
052     * @param c the collection containing objects to {@link #init init}.
053     * @throws ShiroException if unable to initialize one or more instances.
054     * @since 0.9
055     */
056    public static void init(Collection c) throws ShiroException {
057        if (c == null || c.isEmpty()) {
058            return;
059        }
060        for (Object o : c) {
061            init(o);
062        }
063    }
064
065    public static void destroy(Object o) {
066        if (o instanceof Destroyable) {
067            destroy((Destroyable) o);
068        } else if (o instanceof Collection) {
069            destroy((Collection)o);
070        }
071    }
072
073    public static void destroy(Destroyable d) {
074        if (d != null) {
075            try {
076                d.destroy();
077            } catch (Throwable t) {
078                if (log.isDebugEnabled()) {
079                    String msg = "Unable to cleanly destroy instance [" + d + "] of type [" + d.getClass().getName() + "].";
080                    log.debug(msg, t);
081                }
082            }
083        }
084    }
085
086    /**
087     * Calls {@link #destroy(Object) destroy} for each object in the collection.
088     * If the collection is {@code null} or empty, this method returns quietly.
089     *
090     * @param c the collection of objects to destroy.
091     * @since 0.9
092     */
093    public static void destroy(Collection c) {
094        if (c == null || c.isEmpty()) {
095            return;
096        }
097
098        for (Object o : c) {
099            destroy(o);
100        }
101    }
102}