1/*
2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26
27
28package com.sun.org.glassfish.external.probe.provider;
29
30import java.util.Vector;
31
32/**
33 *
34 * @author abbagani
35 */
36public class StatsProviderManager {
37
38   private StatsProviderManager(){
39   }
40
41
42   public static boolean register(String configElement, PluginPoint pp,
43                                    String subTreeRoot, Object statsProvider) {
44        return (register(pp, configElement, subTreeRoot, statsProvider, null));
45   }
46
47   public static boolean register(PluginPoint pp, String configElement,
48                                  String subTreeRoot, Object statsProvider,
49                                  String invokerId) {
50        StatsProviderInfo spInfo =
51            new StatsProviderInfo(configElement, pp, subTreeRoot, statsProvider, invokerId);
52
53        return registerStatsProvider(spInfo);
54   }
55
56   public static boolean register(String configElement, PluginPoint pp,
57                                    String subTreeRoot, Object statsProvider,
58                                    String configLevelStr) {
59        return(register(configElement, pp, subTreeRoot, statsProvider, configLevelStr, null));
60   }
61
62   public static boolean register(String configElement, PluginPoint pp,
63                                    String subTreeRoot, Object statsProvider,
64                                    String configLevelStr, String invokerId) {
65        StatsProviderInfo spInfo =
66            new StatsProviderInfo(configElement, pp, subTreeRoot, statsProvider, invokerId);
67        spInfo.setConfigLevel(configLevelStr);
68
69        return registerStatsProvider(spInfo);
70   }
71
72   private static boolean registerStatsProvider(StatsProviderInfo spInfo) {
73      //Ideally want to start this in a thread, so we can reduce the startup time
74      if (spmd == null) {
75          //Make an entry into the toBeRegistered map
76          toBeRegistered.add(spInfo);
77      } else {
78          spmd.register(spInfo);
79          return true;
80      }
81       return false;
82   }
83
84   public static boolean unregister(Object statsProvider) {
85      //Unregister the statsProvider if the delegate is not null
86      if (spmd == null) {
87          for (StatsProviderInfo spInfo : toBeRegistered) {
88              if (spInfo.getStatsProvider() == statsProvider) {
89                  toBeRegistered.remove(spInfo);
90                  break;
91              }
92          }
93
94      } else {
95          spmd.unregister(statsProvider);
96          return true;
97      }
98       return false;
99   }
100
101
102   public static boolean hasListeners(String probeStr) {
103      //See if the probe has any listeners registered
104      if (spmd == null) {
105          return false;
106      } else {
107          return spmd.hasListeners(probeStr);
108      }
109   }
110
111
112   public static void setStatsProviderManagerDelegate(
113                                    StatsProviderManagerDelegate lspmd) {
114      //System.out.println("in StatsProviderManager.setStatsProviderManagerDelegate ***********");
115      if (lspmd == null) {
116          //Should log and throw an exception
117          return;
118      }
119
120      //Assign the Delegate
121      spmd = lspmd;
122
123      //System.out.println("Running through the toBeRegistered array to call register ***********");
124
125      //First register the pending StatsProviderRegistryElements
126      for (StatsProviderInfo spInfo : toBeRegistered) {
127          spmd.register(spInfo);
128      }
129
130      //Now that you registered the pending calls, Clear the toBeRegistered store
131      toBeRegistered.clear();
132   }
133
134   //variables
135   static StatsProviderManagerDelegate spmd; // populate this during our initilaization process
136   static Vector<StatsProviderInfo> toBeRegistered = new Vector();
137}
138