View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * A silly utility class to help marking stuff as deprecated even if they're not used directly
41   * (taglib attributes for instance).
42   * Logs in std out, std err, the caller's logger, and this class' logger too.
43   */
44  public class DeprecationUtil {
45      private static final Logger log = LoggerFactory.getLogger(DeprecationUtil.class);
46      private static final int MAX_STACKTRACE_ELEMENTS = 3;
47  
48      public static void isDeprecated() {
49          internal_isDeprecated("No reason given");
50      }
51  
52      public static void isDeprecated(String reason) {
53          internal_isDeprecated(reason);
54      }
55  
56      private static void internal_isDeprecated(String reason) {
57          final StringBuffer out = new StringBuffer("A deprecated class or method was used: ");
58          out.append(reason);
59          out.append(". Check the following trace: ");
60          final Throwable fakeException = new Throwable();
61          final StackTraceElement[] elements = fakeException.getStackTrace();
62          StackTraceElement el = null;
63          // we start at 2 since we don't need the latest elements (ie the call to DeprecationUtil)
64          for (int i = 2; (i < elements.length && i <= MAX_STACKTRACE_ELEMENTS); i++) {
65              el = elements[i];
66              out.append(el);
67              out.append(", ");
68          }
69          out.append("the full stacktrace will be logged in debug mode in the ");
70          out.append(log.getName());
71          out.append(" category.");
72          final String outStr = out.toString();
73          if (el != null) {
74              final Logger theClassLog = LoggerFactory.getLogger(el.getClassName());
75              theClassLog.warn(outStr);
76          }
77          log.warn(outStr);
78          log.debug(outStr, fakeException);
79          System.err.println(outStr);
80          System.out.println(outStr);
81      }
82  }