01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.usecase.impl;
19:
20: import java.util.Iterator;
21: import java.util.SortedSet;
22: import java.util.TreeSet;
23:
24: import org.apache.avalon.framework.activity.Startable;
25: import org.apache.avalon.framework.configuration.Configuration;
26: import org.apache.avalon.framework.configuration.ConfigurationException;
27: import org.apache.avalon.framework.service.ServiceException;
28: import org.apache.avalon.framework.service.ServiceManager;
29: import org.apache.avalon.framework.service.Serviceable;
30: import org.apache.avalon.framework.thread.ThreadSafe;
31: import org.apache.cocoon.components.ExtendedComponentSelector;
32: import org.apache.lenya.cms.usecase.UsecaseResolver;
33:
34: /**
35: * Usecase selector.
36: */
37: public class UsecaseSelector extends ExtendedComponentSelector
38: implements ThreadSafe, Startable, Serviceable {
39:
40: private SortedSet usecaseNames;
41: private ServiceManager manager;
42:
43: /**
44: * @return The names of all registered usecases in alphabetical order.
45: */
46: public String[] getUsecaseNames() {
47: return (String[]) this .usecaseNames
48: .toArray(new String[this .usecaseNames.size()]);
49: }
50:
51: public void configure(Configuration config)
52: throws ConfigurationException {
53: super .configure(config);
54:
55: this .usecaseNames = new TreeSet();
56: Configuration[] usecaseConfigs = config
57: .getChildren("component-instance");
58: for (int i = 0; i < usecaseConfigs.length; i++) {
59: this .usecaseNames.add(usecaseConfigs[i]
60: .getAttribute("name"));
61: }
62: }
63:
64: public void start() throws Exception {
65: UsecaseResolver resolver = null;
66: try {
67: resolver = (UsecaseResolver) this .manager
68: .lookup(UsecaseResolver.ROLE);
69: for (Iterator i = this .usecaseNames.iterator(); i.hasNext();) {
70: resolver.register((String) i.next());
71: }
72: } finally {
73: if (resolver != null) {
74: this .manager.release(resolver);
75: }
76: }
77: }
78:
79: public void stop() throws Exception {
80: }
81:
82: public void service(ServiceManager manager) throws ServiceException {
83: this.manager = manager;
84: }
85:
86: }
|