blob: 9ebf4d38b4f5487c9f31a36a09993002c4e39ec3 [file] [log] [blame]
Francesco Furfarid8bdb642006-04-04 23:33:40 +00001/*
2 * Copyright 2006 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.felix.upnp.basedriver.export;
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Iterator;
22import java.util.Vector;
23
24import org.osgi.framework.BundleContext;
25import org.osgi.framework.ServiceReference;
26import org.osgi.service.upnp.UPnPDevice;
27
Francesco Furfarif2a67912006-07-17 17:08:02 +000028/*
29* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
30*/
Francesco Furfarid8bdb642006-04-04 23:33:40 +000031public class DeviceNode {
32
33 private ServiceReference sr;
34 private boolean isRootNode;
35 private String udn ;
36 private boolean hasChild;
37 private int numberOfSons;
38 private ArrayList children;
39 private DeviceNode parent;
40
41 public DeviceNode(ServiceReference sr){
42 //PRE: argument is always UPnPDevice service reference
43 if (sr == null)
44 throw new IllegalArgumentException("null is not a valid arg in DeviceNode constructor");
45 this.sr = sr;
46 udn = (String) sr.getProperty(UPnPDevice.UDN);
47 parent=null;
48 isRootNode = (sr.getProperty(UPnPDevice.PARENT_UDN) == null);
49 String[] sons = ((String[]) sr.getProperty(UPnPDevice.CHILDREN_UDN));
50 hasChild = (sons != null);
51 if (hasChild) {
52 children = new ArrayList();
53 numberOfSons = sons.length;
54 }
55 }
56
57 public ServiceReference getReference(){
58 return sr;
59 }
60 public UPnPDevice getDevice(BundleContext ctx){
61 return (UPnPDevice)ctx.getService(sr);
62 }
63
64 public void attach(DeviceNode node){
65 if (node == null)
66 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.attach() method");
67 node.parent = this;
68 children.add(node);
69 }
70
71 public DeviceNode dethatch(String name){
72 DeviceNode dn = this.search(name);
73 if(dn==null)
74 return null;
75
76 if(dn.parent!=null){
77 Iterator list = dn.parent.children.iterator();
78 while (list.hasNext()) {
79 DeviceNode x = (DeviceNode) list.next();
80 if(x.udn.equals(name)){
81 list.remove();
82 break;
83 }
84 }
85 }
86 dn.parent=null;
87 return dn;
88 }
89
90 public Collection getAllChildren(){
91 if((this.children==null)||(this.children.size()==0))
92 return null;
93
94 Vector v = new Vector(this.children);
95 Iterator list = this.children.iterator();
96 while (list.hasNext()) {
97 DeviceNode x = (DeviceNode) list.next();
98 Collection c = x.getAllChildren();
99 if(c==null) continue;
100 v.addAll(c);
101 }
102 return v;
103 }
104
105 public Collection getChildren(){
106 if((this.children==null)||(this.children.size()==0))
107 return null;
108 return this.children;
109 }
110
111 /**
112 * @param name <code>String</code> that contain the UDN to look for
113 * @return return a <code>DeviceNode</code> that have the UDN equals to name and <br>
114 * if there is any <code>DeviceNode</code> with the proper UDN value return <code>null</code>
115 */
116 public DeviceNode search(String name){
117 if (name == null)
118 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.search() method");
119 if (name.equals(udn))
120 return this;
121 else if (hasChild){
122 Iterator list = children.iterator();
123 while (list.hasNext()){
124 DeviceNode child = (DeviceNode)list.next();
125 DeviceNode node = child.search(name);
126 if (node != null) return node;
127 }
128 }
129 return null;
130 }
131
132 /**
133 *
134 * @param udn
135 * @return <code>true</code> if and only if this <code>DeviceNode</code>
136 * contains a DeviceNode with UDN equals to passed argument or if
137 * its USN is equal to passed argument
138 */
139 public boolean contains(String udn){
140 return this.search(udn)!=null;
141 }
142
143 public boolean isComplete(){
144 if (! hasChild) return true;
145 if (numberOfSons != children.size())return false;
146 Iterator list = children.iterator();
147 while (list.hasNext()){
148 DeviceNode child = (DeviceNode)list.next();
149 if (! child.isComplete()) return false;
150 }
151 return true;
152 }
153
154 public DeviceNode isAttachable(DeviceNode node){
155 if (node == null)
156 throw new IllegalArgumentException("null is not a valid arg in DeviceNode.isAttachable() method");
157 String parentUDN=(String) node.getReference().getProperty(UPnPDevice.PARENT_UDN);
158 if(parentUDN==null) return null;
159 return search(parentUDN);
160 }
161
162 public boolean isRoot(){
163 return isRootNode;
164 }
165
166 public boolean equals(String udn){
167 return this.udn.equals(udn);
168 }
169
170 public String toString(){
171 return udn;
172 }
173
174 /**
175 * @return true if and only
176 * if the Device doesn't have embedded Device
177 */
178 public boolean isLeaf() {
179 return !hasChild;
180 }
181}