Marcel Offermans | 613a620 | 2009-03-11 22:31:44 +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.das; |
| 20 | |
| 21 | |
| 22 | import org.apache.felix.das.util.DriverLoader; |
| 23 | import org.apache.felix.das.util.Util; |
| 24 | import org.osgi.framework.Bundle; |
| 25 | import org.osgi.framework.BundleException; |
| 26 | import org.osgi.framework.ServiceReference; |
| 27 | import org.osgi.service.device.Constants; |
| 28 | import org.osgi.service.device.Driver; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * TODO: add javadoc |
| 33 | * |
| 34 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 35 | */ |
| 36 | public class DriverAttributes |
| 37 | { |
| 38 | |
| 39 | private final Bundle m_bundle; |
| 40 | |
| 41 | private final ServiceReference m_ref; |
| 42 | |
| 43 | private final Driver m_driver; |
| 44 | |
| 45 | private final boolean m_dynamic; |
| 46 | |
| 47 | public DriverAttributes( ServiceReference ref, Driver driver ) |
| 48 | { |
| 49 | m_ref = ref; |
| 50 | m_driver = driver; |
| 51 | m_bundle = ref.getBundle(); |
| 52 | m_dynamic = m_bundle.getLocation().startsWith( DriverLoader.DRIVER_LOCATION_PREFIX ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | public ServiceReference getReference() |
| 57 | { |
| 58 | return m_ref; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | public String getDriverId() |
| 63 | { |
| 64 | return m_ref.getProperty( Constants.DRIVER_ID ).toString(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | public int match( ServiceReference ref ) throws Exception |
| 69 | { |
| 70 | return m_driver.match( ref ); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * a driver bundle is idle if it isn't connected to a device bundle. |
| 76 | * |
| 77 | * @return |
| 78 | */ |
| 79 | private boolean isInUse() |
| 80 | { |
| 81 | ServiceReference[] used = m_bundle.getServicesInUse(); |
| 82 | if ( used == null || used.length == 0 ) |
| 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | for ( ServiceReference ref : used ) |
| 88 | { |
Stuart McCulloch | 0a0508c | 2009-04-16 09:41:41 +0000 | [diff] [blame] | 89 | if ( Util.isDevice( ref ) ) |
Marcel Offermans | 613a620 | 2009-03-11 22:31:44 +0000 | [diff] [blame] | 90 | { |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public String attach( ServiceReference ref ) throws Exception |
| 99 | { |
| 100 | return m_driver.attach( ref ); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public void tryUninstall() throws BundleException |
| 105 | { |
Stuart McCulloch | 0a0508c | 2009-04-16 09:41:41 +0000 | [diff] [blame] | 106 | |
| 107 | // only install if _we_ loaded the driver |
| 108 | if ( !isInUse() && m_dynamic ) |
Marcel Offermans | 613a620 | 2009-03-11 22:31:44 +0000 | [diff] [blame] | 109 | { |
Stuart McCulloch | 0a0508c | 2009-04-16 09:41:41 +0000 | [diff] [blame] | 110 | m_bundle.uninstall(); |
Marcel Offermans | 613a620 | 2009-03-11 22:31:44 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | } |