001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.runtime.bean;
020:
021: import java.util.ArrayList;
022: import java.util.Set;
023: import java.util.HashSet;
024: import java.io.BufferedReader;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.IOException;
028:
029: /**
030: * Class used to support prioritizing interceptors on methods of a {@link ControlBean}.
031: */
032: public final class InterceptorUtils {
033:
034: private InterceptorUtils() {
035: }
036:
037: /**
038: * Filename that contains ordering priority for controls interceptor services.
039: * Each line in the file is a fully qualified interface name. The first line in the file
040: * is highest priority.
041: */
042: public static final String INTERCEPTOR_CONFIG_FILE = "controls-interceptors.config";
043:
044: // todo: this interceptor priority list should be stored by ClassLoader instead of shared between them
045: /**
046: * List that keeps track of the interceptors in their priority order.
047: */
048: private static ArrayList<String> _interceptorPriorities;
049:
050: /**
051: * Applies externally defined (via {@link #INTERCEPTOR_CONFIG_FILE}) ordering priority for
052: * controls interceptor services.
053: *
054: * @param interceptors
055: * @return String[]
056: */
057: public static String[] prioritizeInterceptors(String[] interceptors) {
058: if (interceptors == null)
059: return null;
060:
061: // Read external configuration to obtain desired prioritization.
062: if (_interceptorPriorities == null) {
063: // Only attempt to read the external configuration once; bounce the VM if you
064: // want to try again.
065: _interceptorPriorities = new ArrayList<String>();
066: BufferedReader in = null;
067: try {
068: InputStream configFileStream = ControlBeanContext.class
069: .getClassLoader().getResourceAsStream(
070: INTERCEPTOR_CONFIG_FILE);
071:
072: if (configFileStream != null) {
073: in = new BufferedReader(new InputStreamReader(
074: configFileStream));
075: String str;
076: while ((str = in.readLine()) != null)
077: _interceptorPriorities.add(str);
078: }
079: } catch (IOException e) {
080: // ignore
081: } finally {
082: try {
083: if (in != null)
084: in.close();
085: } catch (IOException ie) { /* ignore */
086: }
087: }
088: }
089:
090: // Put input list of interceptors into a Set for easy lookup
091: Set<String> input = new HashSet<String>();
092: for (String ii : interceptors)
093: input.add(ii);
094:
095: // Scan through priorities list, building a prioritized list
096: ArrayList<String> prioritized = new ArrayList<String>(
097: interceptors.length);
098: for (String p : _interceptorPriorities) {
099: if (input.contains(p)) {
100: input.remove(p);
101: prioritized.add(p);
102: }
103: }
104:
105: // Anything still left in the input set did not have a priority associated with it,
106: // so they just go at the end in arbitrary order.
107: for (String p : input)
108: prioritized.add(p);
109:
110: return prioritized.toArray(new String[prioritized.size()]);
111: }
112: }
|