blob: 95429ae53171828b2a493a0b1ffe44f57d27d34c [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
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
Francesco Furfarid8bdb642006-04-04 23:33:40 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid8bdb642006-04-04 23:33:40 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * 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.
Francesco Furfarid8bdb642006-04-04 23:33:40 +000018 */
19
20package org.apache.felix.upnp.basedriver.export;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.Vector;
25
26import org.osgi.framework.BundleContext;
27import org.osgi.framework.ServiceReference;
28import org.osgi.service.upnp.UPnPDevice;
29
Francesco Furfarif2a67912006-07-17 17:08:02 +000030/*
Karl Paulsd312acc2007-06-18 20:38:33 +000031* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarif2a67912006-07-17 17:08:02 +000032*/
Francesco Furfarid8bdb642006-04-04 23:33:40 +000033public class DeviceNode {
34
35 private ServiceReference sr;
36 private boolean isRootNode;
37 private String udn ;
38 private boolean hasChild;
39 private int numberOfSons;
40 private ArrayList children;
41 private DeviceNode parent;
42
43 public DeviceNode(ServiceReference sr){
44 //PRE: argument is always UPnPDevice service reference
45 if (sr == null)
46 throw new IllegalArgumentException("null is not a valid arg in DeviceNode constructor");
47 this.sr = sr;
48 udn = (String) sr.getProperty(UPnPDevice.UDN);
49 parent=null;
50 isRootNode = (sr.getProperty(UPnPDevice.PARENT_UDN) == null);
51 String[] sons = ((String[]) sr.getProperty(UPnPDevice.CHILDREN_UDN));
52 hasChild = (sons != null);
53 if (hasChild) {
54 children = new ArrayList();
55 numberOfSons = sons.length;
56 }
57 }
58
59 public ServiceReference getReference(){
60 return sr;
61 }
62 public UPnPDevice getDevice(BundleContext ctx){
63 return (UPnPDevice)ctx.getService(sr);
64 }
65
66 public void attach(DeviceNode node){
67 if (node == null)
68 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.attach() method");
69 node.parent = this;
70 children.add(node);
71 }
72
73 public DeviceNode dethatch(String name){
74 DeviceNode dn = this.search(name);
75 if(dn==null)
76 return null;
77
78 if(dn.parent!=null){
79 Iterator list = dn.parent.children.iterator();
80 while (list.hasNext()) {
81 DeviceNode x = (DeviceNode) list.next();
82 if(x.udn.equals(name)){
83 list.remove();
84 break;
85 }
86 }
87 }
88 dn.parent=null;
89 return dn;
90 }
91
92 public Collection getAllChildren(){
93 if((this.children==null)||(this.children.size()==0))
94 return null;
95
96 Vector v = new Vector(this.children);
97 Iterator list = this.children.iterator();
98 while (list.hasNext()) {
99 DeviceNode x = (DeviceNode) list.next();
100 Collection c = x.getAllChildren();
101 if(c==null) continue;
102 v.addAll(c);
103 }
104 return v;
105 }
106
107 public Collection getChildren(){
108 if((this.children==null)||(this.children.size()==0))
109 return null;
110 return this.children;
111 }
112
113 /**
114 * @param name <code>String</code> that contain the UDN to look for
115 * @return return a <code>DeviceNode</code> that have the UDN equals to name and <br>
116 * if there is any <code>DeviceNode</code> with the proper UDN value return <code>null</code>
117 */
118 public DeviceNode search(String name){
119 if (name == null)
120 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.search() method");
121 if (name.equals(udn))
122 return this;
123 else if (hasChild){
124 Iterator list = children.iterator();
125 while (list.hasNext()){
126 DeviceNode child = (DeviceNode)list.next();
127 DeviceNode node = child.search(name);
128 if (node != null) return node;
129 }
130 }
131 return null;
132 }
133
134 /**
135 *
136 * @param udn
137 * @return <code>true</code> if and only if this <code>DeviceNode</code>
138 * contains a DeviceNode with UDN equals to passed argument or if
139 * its USN is equal to passed argument
140 */
141 public boolean contains(String udn){
142 return this.search(udn)!=null;
143 }
144
145 public boolean isComplete(){
146 if (! hasChild) return true;
147 if (numberOfSons != children.size())return false;
148 Iterator list = children.iterator();
149 while (list.hasNext()){
150 DeviceNode child = (DeviceNode)list.next();
151 if (! child.isComplete()) return false;
152 }
153 return true;
154 }
155
156 public DeviceNode isAttachable(DeviceNode node){
157 if (node == null)
158 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.isAttachable() method");
159 String parentUDN=(String) node.getReference().getProperty(UPnPDevice.PARENT_UDN);
160 if(parentUDN==null) return null;
161 return search(parentUDN);
162 }
163
164 public boolean isRoot(){
165 return isRootNode;
166 }
167
168 public boolean equals(String udn){
169 return this.udn.equals(udn);
170 }
171
172 public String toString(){
173 return udn;
174 }
175
176 /**
177 * @return true if and only
178 * if the Device doesn't have embedded Device
179 */
180 public boolean isLeaf() {
181 return !hasChild;
182 }
183}