Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dm.runtime; |
| 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.apache.felix.dm.Component; |
| 25 | import org.apache.felix.dm.DependencyManager; |
| 26 | import org.osgi.framework.Bundle; |
| 27 | |
| 28 | /** |
| 29 | * Class used to build a factory configuration adapter service using metadata found from DependencyManager runtime |
| 30 | * meta-inf descriptor. |
| 31 | * |
| 32 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 33 | */ |
| 34 | public class FactoryConfigurationAdapterServiceBuilder extends AbstractBuilder |
| 35 | { |
| 36 | private final static String TYPE = "FactoryConfigurationAdapterService"; |
| 37 | |
| 38 | @Override |
| 39 | public String getType() |
| 40 | { |
| 41 | return TYPE; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void build(MetaData srvMeta, List<MetaData> depsMeta, Bundle b, DependencyManager dm) |
| 46 | throws Exception |
| 47 | { |
| 48 | Class<?> implClass = b.loadClass(srvMeta.getString(Params.impl)); |
| 49 | String factoryPid = srvMeta.getString(Params.factoryPid); |
| 50 | String updated = srvMeta.getString(Params.updated); |
| 51 | String[] provides = srvMeta.getStrings(Params.provides, null); |
| 52 | Dictionary<String, Object> properties = srvMeta.getDictionary(Params.properties, null); |
| 53 | boolean propagate = "true".equals(srvMeta.getString(Params.propagate, "false")); |
Pierre De Rop | 9e5cdba | 2016-02-17 20:35:16 +0000 | [diff] [blame^] | 54 | String configProxyClassName = srvMeta.getString(Params.configType, null); |
Pierre De Rop | 9e5db82 | 2016-02-10 17:37:02 +0000 | [diff] [blame] | 55 | Component c = null; |
| 56 | |
| 57 | if (configProxyClassName != null) |
| 58 | { |
| 59 | Class<?> configProxyClass = b.loadClass(configProxyClassName); |
| 60 | c = dm.createFactoryConfigurationAdapterService(factoryPid, updated, propagate, configProxyClass); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | c = dm.createFactoryConfigurationAdapterService(factoryPid, updated, propagate); |
| 65 | } |
| 66 | |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 67 | c.setInterface(provides, properties); |
| 68 | String factoryMethod = srvMeta.getString(Params.factoryMethod, null); |
Pierre De Rop | 9e5db82 | 2016-02-10 17:37:02 +0000 | [diff] [blame] | 69 | |
| 70 | |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 71 | if (factoryMethod == null) |
| 72 | { |
| 73 | c.setImplementation(implClass); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | c.setFactory(implClass, factoryMethod); |
| 78 | } |
| 79 | setCommonServiceParams(c, srvMeta); |
| 80 | c.setComposition(srvMeta.getString(Params.composition, null)); |
| 81 | ServiceLifecycleHandler lfcleHandler = new ServiceLifecycleHandler(c, b, dm, srvMeta, depsMeta); |
| 82 | // The dependencies will be plugged by our lifecycle handler. |
| 83 | c.setCallbacks(lfcleHandler, "init", "start", "stop", "destroy"); |
| 84 | // Adds dependencies (except named dependencies, which are managed by the lifecycle handler). |
| 85 | addUnamedDependencies(b, dm, c, srvMeta, depsMeta); |
| 86 | dm.add(c); |
| 87 | } |
| 88 | } |