Richard S. Hall | 85bafab | 2009-07-13 13:25: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 | |
| 20 | package org.cauldron.bld.core.internal.model.osgi; |
| 21 | |
| 22 | import java.net.URI; |
| 23 | import java.util.Collection; |
| 24 | import java.util.Collections; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | import org.cauldron.sigil.model.AbstractCompoundModelElement; |
| 29 | import org.cauldron.sigil.model.InvalidModelException; |
| 30 | import org.cauldron.sigil.model.eclipse.ILibraryImport; |
| 31 | import org.cauldron.sigil.model.osgi.IBundleModelElement; |
| 32 | import org.cauldron.sigil.model.osgi.IPackageExport; |
| 33 | import org.cauldron.sigil.model.osgi.IPackageImport; |
| 34 | import org.cauldron.sigil.model.osgi.IRequiredBundle; |
| 35 | import org.osgi.framework.Version; |
| 36 | |
| 37 | public class BundleModelElement extends AbstractCompoundModelElement implements IBundleModelElement { |
| 38 | /** |
| 39 | * |
| 40 | */ |
| 41 | private static final long serialVersionUID = 1L; |
| 42 | // required obr values |
| 43 | private URI updateLocation; |
| 44 | private String symbolicName; |
| 45 | private Version version = Version.emptyVersion; |
| 46 | private Set<IPackageImport> imports; |
| 47 | private Set<IPackageExport> exports; |
| 48 | private Set<IRequiredBundle> requires; |
| 49 | private URI sourceLocation; |
| 50 | private Set<String> classpathElements; |
| 51 | private IRequiredBundle fragmentHost; |
| 52 | |
| 53 | // human readable values |
| 54 | private String name; |
| 55 | private String description; |
| 56 | private String category; |
| 57 | private URI licenseURI; |
| 58 | private URI docURI; |
| 59 | private String vendor; |
| 60 | private String contactAddress; |
| 61 | private String copyright; |
| 62 | |
| 63 | // internal values |
| 64 | private String activator; |
| 65 | private Set<ILibraryImport> libraries; |
| 66 | |
| 67 | public BundleModelElement() { |
| 68 | super( "OSGi Bundle" ); |
| 69 | this.imports = new HashSet<IPackageImport>(); |
| 70 | this.exports = new HashSet<IPackageExport>(); |
| 71 | this.requires = new HashSet<IRequiredBundle>(); |
| 72 | this.classpathElements = new HashSet<String>(); |
| 73 | this.libraries = new HashSet<ILibraryImport>(); |
| 74 | } |
| 75 | |
| 76 | public String getActivator() { |
| 77 | return activator; |
| 78 | } |
| 79 | |
| 80 | public void setActivator(String activator) { |
| 81 | this.activator = activator; |
| 82 | } |
| 83 | |
| 84 | public void addLibraryImport(ILibraryImport library) { |
| 85 | libraries.add(library); |
| 86 | } |
| 87 | |
| 88 | public Set<ILibraryImport> getLibraryImports() { |
| 89 | return libraries; |
| 90 | } |
| 91 | |
| 92 | public void removeLibraryImport(ILibraryImport library) { |
| 93 | libraries.remove(library); |
| 94 | } |
| 95 | |
| 96 | /* (non-Javadoc) |
| 97 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getCategory() |
| 98 | */ |
| 99 | public String getCategory() { |
| 100 | return category; |
| 101 | } |
| 102 | |
| 103 | /* (non-Javadoc) |
| 104 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setCategory(java.lang.String) |
| 105 | */ |
| 106 | public void setCategory(String category) { |
| 107 | this.category = category; |
| 108 | } |
| 109 | |
| 110 | /* (non-Javadoc) |
| 111 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getContactAddress() |
| 112 | */ |
| 113 | public String getContactAddress() { |
| 114 | return contactAddress; |
| 115 | } |
| 116 | |
| 117 | /* (non-Javadoc) |
| 118 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setContactAddress(java.lang.String) |
| 119 | */ |
| 120 | public void setContactAddress(String contactAddress) { |
| 121 | this.contactAddress = contactAddress; |
| 122 | } |
| 123 | |
| 124 | /* (non-Javadoc) |
| 125 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getCopyright() |
| 126 | */ |
| 127 | public String getCopyright() { |
| 128 | return copyright; |
| 129 | } |
| 130 | |
| 131 | /* (non-Javadoc) |
| 132 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setCopyright(java.lang.String) |
| 133 | */ |
| 134 | public void setCopyright(String copyright) { |
| 135 | this.copyright = copyright; |
| 136 | } |
| 137 | |
| 138 | /* (non-Javadoc) |
| 139 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getDocURI() |
| 140 | */ |
| 141 | public URI getDocURI() { |
| 142 | return docURI; |
| 143 | } |
| 144 | |
| 145 | /* (non-Javadoc) |
| 146 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setDocURI(java.net.URI) |
| 147 | */ |
| 148 | public void setDocURI(URI docURI) { |
| 149 | this.docURI = docURI; |
| 150 | } |
| 151 | |
| 152 | /* (non-Javadoc) |
| 153 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getExports() |
| 154 | */ |
| 155 | public Set<IPackageExport> getExports() { |
| 156 | return exports; |
| 157 | } |
| 158 | |
| 159 | /* (non-Javadoc) |
| 160 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#addExport(org.cauldron.sigil.model.osgi.PackageExport) |
| 161 | */ |
| 162 | public void addExport(IPackageExport packageExport) { |
| 163 | exports.add(packageExport); |
| 164 | packageExport.setParent(this); |
| 165 | } |
| 166 | |
| 167 | /* (non-Javadoc) |
| 168 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#removeExport(org.cauldron.sigil.model.osgi.PackageExport) |
| 169 | */ |
| 170 | public void removeExport(IPackageExport packageExport) { |
| 171 | if ( exports.remove(packageExport) ) { |
| 172 | packageExport.setParent(null); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* (non-Javadoc) |
| 177 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getImports() |
| 178 | */ |
| 179 | public Set<IPackageImport> getImports() { |
| 180 | return imports; |
| 181 | } |
| 182 | |
| 183 | /* (non-Javadoc) |
| 184 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#addImport(org.cauldron.sigil.model.osgi.PackageImport) |
| 185 | */ |
| 186 | public void addImport(IPackageImport packageImport) { |
| 187 | imports.add(packageImport); |
| 188 | packageImport.setParent(this); |
| 189 | } |
| 190 | |
| 191 | /* (non-Javadoc) |
| 192 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#removeImport(org.cauldron.sigil.model.osgi.PackageImport) |
| 193 | */ |
| 194 | public void removeImport(IPackageImport packageImport) { |
| 195 | if ( imports.remove( packageImport ) ) { |
| 196 | packageImport.setParent(null); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* (non-Javadoc) |
| 201 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getRequiredBundles() |
| 202 | */ |
| 203 | public Set<IRequiredBundle> getRequiredBundles() { |
| 204 | return requires; |
| 205 | } |
| 206 | |
| 207 | /* (non-Javadoc) |
| 208 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#addRequiredBundle(org.cauldron.sigil.model.osgi.RequiresBundle) |
| 209 | */ |
| 210 | public void addRequiredBundle(IRequiredBundle bundle) { |
| 211 | requires.add( bundle ); |
| 212 | bundle.setParent(this); |
| 213 | } |
| 214 | |
| 215 | /* (non-Javadoc) |
| 216 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#removeRequiredBundle(org.cauldron.sigil.model.osgi.RequiresBundle) |
| 217 | */ |
| 218 | public void removeRequiredBundle(IRequiredBundle bundle) { |
| 219 | if ( requires.remove(bundle) ) { |
| 220 | bundle.setParent(null); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* (non-Javadoc) |
| 225 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getLicenseURI() |
| 226 | */ |
| 227 | public URI getLicenseURI() { |
| 228 | return licenseURI; |
| 229 | } |
| 230 | |
| 231 | /* (non-Javadoc) |
| 232 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setLicenseURI(java.net.URI) |
| 233 | */ |
| 234 | public void setLicenseURI(URI licenseURI) { |
| 235 | this.licenseURI = licenseURI; |
| 236 | } |
| 237 | |
| 238 | /* (non-Javadoc) |
| 239 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getSourceLocation() |
| 240 | */ |
| 241 | public URI getSourceLocation() { |
| 242 | return sourceLocation; |
| 243 | } |
| 244 | |
| 245 | /* (non-Javadoc) |
| 246 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setSourceLocation(java.net.URI) |
| 247 | */ |
| 248 | public void setSourceLocation(URI sourceLocation) { |
| 249 | this.sourceLocation = sourceLocation; |
| 250 | } |
| 251 | |
| 252 | /* (non-Javadoc) |
| 253 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getSymbolicName() |
| 254 | */ |
| 255 | public String getSymbolicName() { |
| 256 | return symbolicName; |
| 257 | } |
| 258 | |
| 259 | /* (non-Javadoc) |
| 260 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setSymbolicName(java.lang.String) |
| 261 | */ |
| 262 | public void setSymbolicName(String symbolicName) { |
| 263 | this.symbolicName = symbolicName == null ? null : symbolicName.intern(); |
| 264 | } |
| 265 | |
| 266 | /* (non-Javadoc) |
| 267 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getUpdateLocation() |
| 268 | */ |
| 269 | public URI getUpdateLocation() { |
| 270 | return updateLocation; |
| 271 | } |
| 272 | |
| 273 | /* (non-Javadoc) |
| 274 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setUpdateLocation(java.net.URI) |
| 275 | */ |
| 276 | public void setUpdateLocation(URI updateLocation) { |
| 277 | this.updateLocation = updateLocation; |
| 278 | } |
| 279 | |
| 280 | /* (non-Javadoc) |
| 281 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getVendor() |
| 282 | */ |
| 283 | public String getVendor() { |
| 284 | return vendor; |
| 285 | } |
| 286 | |
| 287 | /* (non-Javadoc) |
| 288 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setVendor(java.lang.String) |
| 289 | */ |
| 290 | public void setVendor(String vendor) { |
| 291 | this.vendor = vendor; |
| 292 | } |
| 293 | |
| 294 | /* (non-Javadoc) |
| 295 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#getVersion() |
| 296 | */ |
| 297 | public Version getVersion() { |
| 298 | return version; |
| 299 | } |
| 300 | |
| 301 | /* (non-Javadoc) |
| 302 | * @see org.cauldron.sigil.model.osgi.IBundleModelElement#setVersion(java.lang.String) |
| 303 | */ |
| 304 | public void setVersion(Version version) { |
| 305 | this.version = version == null ? Version.emptyVersion : version; |
| 306 | } |
| 307 | |
| 308 | public void checkValid() throws InvalidModelException { |
| 309 | if (symbolicName == null) |
| 310 | throw new InvalidModelException(this, "Bundle symbolic name not set"); |
| 311 | } |
| 312 | |
| 313 | public BundleModelElement clone() { |
| 314 | BundleModelElement bd = (BundleModelElement) super.clone(); |
| 315 | |
| 316 | bd.imports = new HashSet<IPackageImport>(); |
| 317 | bd.exports = new HashSet<IPackageExport>(); |
| 318 | bd.requires = new HashSet<IRequiredBundle>(); |
| 319 | |
| 320 | for (IPackageImport pi : imports ) { |
| 321 | bd.imports.add((IPackageImport) pi.clone()); |
| 322 | } |
| 323 | |
| 324 | for (IPackageExport pe : exports ) { |
| 325 | bd.exports.add((IPackageExport) pe.clone()); |
| 326 | } |
| 327 | |
| 328 | for ( IRequiredBundle rb : requires ) { |
| 329 | bd.requires.add((IRequiredBundle) rb.clone()); |
| 330 | } |
| 331 | |
| 332 | return bd; |
| 333 | } |
| 334 | |
| 335 | public String toString() { |
| 336 | StringBuffer buf = new StringBuffer(); |
| 337 | |
| 338 | buf.append("BundleModelElement["); |
| 339 | buf.append(symbolicName); |
| 340 | buf.append(", "); |
| 341 | buf.append(version); |
| 342 | buf.append("]"); |
| 343 | |
| 344 | return buf.toString(); |
| 345 | } |
| 346 | |
| 347 | public String getName() { |
| 348 | return name; |
| 349 | } |
| 350 | |
| 351 | public void setName(String name) { |
| 352 | this.name = name; |
| 353 | } |
| 354 | |
| 355 | public String getDescription() { |
| 356 | return description; |
| 357 | } |
| 358 | |
| 359 | public void setDescription(String description) { |
| 360 | this.description = description; |
| 361 | } |
| 362 | |
| 363 | public void addClasspath(String path) { |
| 364 | classpathElements.add( path ); |
| 365 | } |
| 366 | |
| 367 | public Collection<String> getClasspaths() { |
| 368 | return classpathElements.isEmpty() ? Collections.singleton( "." ) : classpathElements; |
| 369 | } |
| 370 | |
| 371 | public void removeClasspath(String path) { |
| 372 | classpathElements.remove( path ); |
| 373 | } |
| 374 | |
| 375 | public IRequiredBundle getFragmentHost() { |
| 376 | return fragmentHost; |
| 377 | } |
| 378 | |
| 379 | public void setFragmentHost(IRequiredBundle fragmentHost) { |
| 380 | this.fragmentHost = fragmentHost; |
| 381 | } |
| 382 | } |