1 7 8 package org.jboss.media.entity; 9 10 import java.io.Reader ; 11 import java.io.StringReader ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import javax.ejb.CreateException ; 18 import javax.ejb.EntityBean ; 19 import javax.ejb.EntityContext ; 20 import javax.ejb.FinderException ; 21 import javax.ejb.RemoveException ; 22 import javax.emb.ContentAccessException; 23 import javax.emb.MediaEntityLocal; 24 import javax.emb.MediaException; 25 import javax.emb.MediaFormat; 26 import javax.emb.MetaDataEntityLocal; 27 import javax.emb.MetaDataSyntaxException; 28 import javax.emb.UnsupportedQueryLanguageException; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingException ; 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 34 import org.jboss.ejb.plugins.keygenerator.KeyGenerator; 35 import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory; 36 import org.jboss.logging.Logger; 37 import org.jboss.media.entity.query.JBossMediaQueryLanguage; 38 import org.jboss.media.entity.query.MediaQueryLanguage; 39 import org.w3c.dom.Document ; 40 import org.xml.sax.InputSource ; 41 42 66 public abstract class MetaDataEntityBean implements EntityBean 67 { 68 private EntityContext entityContext; 69 70 71 private Logger log = Logger.getLogger(MetaDataEntityBean.class); 72 73 74 private static final String KEY_GENERATOR_JNDI = "UUIDKeyGeneratorFactory"; 75 76 77 private static final MediaQueryLanguage[] MEDIA_QUERY_LANGUAGES = 78 { new JBossMediaQueryLanguage()}; 79 80 82 86 public abstract String getManagedIdentity(); 87 public abstract void setManagedIdentity(String identity); 88 89 92 public abstract String getManagedXML(); 93 public abstract void setManagedXML(String xml); 94 95 98 public abstract String getManagedName(); 99 public abstract void setManagedName(String name); 100 101 104 public abstract long getManagedLastModified(); 105 public abstract void setManagedLastModified(long lastModified); 106 107 114 public abstract MetaDataEntityLocal getManagedPreviousVersion(); 115 public abstract void setManagedPreviousVersion(MetaDataEntityLocal previousVersion); 116 117 124 public abstract MetaDataEntityLocal getManagedNextVersion(); 125 public abstract void setManagedNextVersion(MetaDataEntityLocal nextVersion); 126 127 134 public abstract Collection getManagedParents(); 135 public abstract void setManagedParents(Collection parents); 136 137 144 public abstract Collection getManagedChildren(); 145 public abstract void setManagedChildren(Collection children); 146 147 154 public abstract Collection getManagedMedias(); 155 public abstract void setManagedMedias(Collection medias); 156 157 159 162 public void addChild(MetaDataEntityLocal child) throws MediaException 163 { 164 if (child == null) 165 { 166 throw new NullPointerException (); 167 } 168 169 if (!this.getManagedChildren().contains(child)) 170 { 171 this.getManagedChildren().add(child); 172 } 173 } 174 175 178 public void addMediaEntity(MediaEntityLocal mediaEntity) 179 throws MediaException 180 { 181 if (mediaEntity == null) 182 { 183 throw new NullPointerException (); 184 } 185 186 if (!this.getManagedMedias().contains(mediaEntity)) 187 { 188 this.getManagedMedias().add(mediaEntity); 189 this.updateLastModified(); 190 } 191 } 192 193 196 public MetaDataEntityLocal[] getChildren() throws MediaException 197 { 198 return (MetaDataEntityLocal[]) this.getManagedChildren().toArray( 199 new MetaDataEntityLocal[0]); 200 } 201 202 205 public long getLastModified() throws MediaException 206 { 207 return this.getManagedLastModified(); 208 } 209 210 213 public MediaEntityLocal[] getMediaEntities() throws MediaException 214 { 215 return (MediaEntityLocal[]) this.getManagedMedias().toArray( 216 new MediaEntityLocal[0]); 217 } 218 219 222 public MediaEntityLocal[] getMediaEntities( 223 MediaFormat mediaFormat, 224 boolean searchChildren) 225 throws MediaException 226 { 227 if (mediaFormat == null) 228 { 229 throw new NullPointerException (); 230 } 231 232 Collection mediaEntities = new ArrayList (); 233 Iterator it = this.getManagedMedias().iterator(); 234 235 while (it.hasNext()) 236 { 237 MediaEntityLocal mediaEntity = (MediaEntityLocal) it.next(); 238 MediaFormat format = mediaEntity.getFormat(); 239 if (mediaFormat == format) 240 { 241 mediaEntities.add(mediaEntity); 242 } 243 } 244 245 if (searchChildren) 246 { 247 MetaDataEntityLocal children[] = this.getChildren(); 248 for (int i = 0; i < children.length; i++) 249 { 250 MediaEntityLocal mediaEntity[] = 251 children[i].getMediaEntities(mediaFormat, searchChildren); 252 253 for (int j = 0; j < mediaEntity.length; j++) 254 { 255 mediaEntities.add(mediaEntity[j]); 256 } 257 } 258 } 259 260 return (MediaEntityLocal[]) mediaEntities.toArray( 261 new MediaEntityLocal[0]); 262 } 263 264 267 public MediaEntityLocal[] getMediaEntities( 268 String mimeType, 269 boolean searchChildren) 270 throws MediaException 271 { 272 if (mimeType == null) 273 { 274 throw new NullPointerException (); 275 } 276 277 Collection mediaEntities = new ArrayList (); 278 Iterator it = this.getManagedMedias().iterator(); 279 280 while (it.hasNext()) 281 { 282 MediaEntityLocal mediaEntity = (MediaEntityLocal) it.next(); 283 String type = mediaEntity.getMimeType(); 284 if (mimeType.equals(type)) 285 { 286 mediaEntities.add(mediaEntity); 287 } 288 } 289 290 if (searchChildren) 291 { 292 MetaDataEntityLocal children[] = this.getChildren(); 293 for (int i = 0; i < children.length; i++) 294 { 295 MediaEntityLocal mediaEntity[] = 296 children[i].getMediaEntities(mimeType, searchChildren); 297 298 for (int j = 0; j < mediaEntity.length; j++) 299 { 300 mediaEntities.add(mediaEntity[j]); 301 } 302 } 303 } 304 305 return (MediaEntityLocal[]) mediaEntities.toArray( 306 new MediaEntityLocal[0]); 307 } 308 309 312 public String getName() throws MediaException 313 { 314 return this.getManagedName(); 315 } 316 317 320 public MetaDataEntityLocal getNextVersion() throws MediaException 321 { 322 return this.getManagedNextVersion(); 323 } 324 325 328 public MetaDataEntityLocal[] getParents() throws MediaException 329 { 330 return (MetaDataEntityLocal[]) this.getManagedParents().toArray( 331 new MetaDataEntityLocal[0]); 332 } 333 334 337 public MetaDataEntityLocal getPreviousVersion() throws MediaException 338 { 339 return this.getManagedPreviousVersion(); 340 } 341 342 345 public String getXML() throws MediaException 346 { 347 String xml = this.getManagedXML(); 348 349 if (xml == null) 350 { 351 throw new ContentAccessException(); 352 } 353 354 return xml; 355 } 356 357 360 public void removeChild(MetaDataEntityLocal child) throws MediaException 361 { 362 if (child == null) 363 { 364 throw new NullPointerException (); 365 } 366 367 if (this.getManagedChildren().contains(child)) 368 { 369 this.getManagedChildren().remove(child); 370 } 371 } 372 373 376 public void removeMediaEntity(MediaEntityLocal mediaEntity) 377 throws MediaException 378 { 379 if (mediaEntity == null) 380 { 381 throw new NullPointerException (); 382 } 383 384 if (this.getManagedMedias().contains(mediaEntity)) 385 { 386 this.getManagedMedias().remove(mediaEntity); 387 } 388 } 389 390 393 public void setName(String name) throws MediaException 394 { 395 if (name == null) 396 { 397 throw new NullPointerException (); 398 } 399 400 this.setManagedName(name); 401 this.updateLastModified(); 402 } 403 404 407 public void setPreviousVersion(MetaDataEntityLocal metadata) 408 throws MediaException 409 { 410 throw new UnsupportedOperationException ("Not yet implemented!"); 412 } 413 414 417 public void setXML(String xmlContent, boolean validate) 418 throws MediaException 419 { 420 if (xmlContent == null) 421 { 422 throw new NullPointerException (); 423 } 424 425 if (validate) 426 { 427 Reader xmlReader = new StringReader (xmlContent); 428 429 try 430 { 431 InputSource xmlSource = new InputSource (xmlReader); 432 DocumentBuilderFactory factory = 433 DocumentBuilderFactory.newInstance(); 434 435 DocumentBuilder builder = factory.newDocumentBuilder(); 436 Document document = builder.parse(xmlSource); 437 438 } 440 catch (Exception e) 441 { 442 throw new MetaDataSyntaxException(e); 444 } 445 } 446 447 this.setManagedXML(xmlContent); 448 this.updateLastModified(); 449 } 450 451 453 456 public String ejbCreate() throws CreateException 457 { 458 String identity = this.generateIdentity(); 459 460 this.setManagedIdentity(identity); 461 this.updateLastModified(); 462 463 return null; 464 } 465 466 469 public void ejbPostCreate() throws CreateException 470 { 471 } 472 473 476 public Collection ejbHomeQuery( 477 String query, 478 String queryLanguage, 479 Map options) 480 throws FinderException , MediaException 481 { 482 if ((query == null) || (queryLanguage == null)) 483 { 484 throw new NullPointerException (); 485 } 486 487 MediaQueryLanguage mediaQueryLanguage = null; 488 489 for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++) 490 { 491 if (queryLanguage.equals(MEDIA_QUERY_LANGUAGES[i].getName())) 492 { 493 mediaQueryLanguage = MEDIA_QUERY_LANGUAGES[i]; 494 } 495 } 496 497 if (mediaQueryLanguage == null) 499 { 500 throw new UnsupportedQueryLanguageException(); 501 } 502 503 mediaQueryLanguage.setOptions(options); 504 505 return mediaQueryLanguage.query(this, query); 506 } 507 508 511 public String [] ejbHomeRetrieveSupportedOptions(String queryLanguage) 512 throws MediaException 513 { 514 if (queryLanguage == null) 515 { 516 throw new NullPointerException (); 517 } 518 519 for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++) 520 { 521 MediaQueryLanguage mediaQueryLanguage = MEDIA_QUERY_LANGUAGES[i]; 522 523 if (queryLanguage.equals(mediaQueryLanguage.getName())) 524 { 525 Map options = mediaQueryLanguage.getOptions(); 526 String [] supportedOptions = new String [options.size()]; 527 528 Iterator it = options.keySet().iterator(); 529 int j = 0; 530 531 while (it.hasNext()) 532 { 533 String option = (String ) it.next(); 534 supportedOptions[j] = option; 535 j++; 536 } 537 538 return supportedOptions; 539 } 540 } 541 542 throw new UnsupportedQueryLanguageException(); 544 } 545 546 549 public String [] ejbHomeRetrieveSupportedQueryLanguages() 550 throws MediaException 551 { 552 String [] supportedQueryLanguages = 553 new String [MEDIA_QUERY_LANGUAGES.length]; 554 555 for (int i = 0; i < MEDIA_QUERY_LANGUAGES.length; i++) 556 { 557 supportedQueryLanguages[i] = MEDIA_QUERY_LANGUAGES[i].getName(); 558 } 559 560 return supportedQueryLanguages; 561 } 562 563 565 568 public abstract Collection ejbSelectByPartialXML(String partialXML) 569 throws FinderException ; 570 571 573 public void ejbActivate() 574 575 { 576 } 577 578 public void ejbPassivate() 579 { 580 } 581 582 public void ejbRemove() throws RemoveException 583 { 584 } 585 586 public void setEntityContext(EntityContext entityContext) 587 { 588 this.entityContext = entityContext; 589 } 590 591 public void unsetEntityContext() 592 { 593 this.entityContext = null; 594 } 595 596 public void ejbLoad() 597 { 598 } 599 600 public void ejbStore() 601 { 602 } 603 604 606 private void updateLastModified() 607 { 608 this.setManagedLastModified(System.currentTimeMillis()); 609 } 610 611 619 private String generateIdentity() throws CreateException 620 { 621 KeyGenerator keyGenerator = null; 622 623 try 624 { 625 KeyGeneratorFactory keyGeneratorFactory = 626 (KeyGeneratorFactory) new InitialContext ().lookup( 627 KEY_GENERATOR_JNDI); 628 629 keyGenerator = keyGeneratorFactory.getKeyGenerator(); 630 } 631 catch (NamingException e) 632 { 633 throw new CreateException ( 634 "Error: can't find key generator factory: " 635 + KEY_GENERATOR_JNDI 636 + "; " 637 + e.getMessage()); 638 } 639 catch (Exception e) 640 { 641 throw new CreateException ( 642 "Error: can't create key generator instance; key generator factory: " 643 + KEY_GENERATOR_JNDI 644 + "; " 645 + e.getMessage()); 646 } 647 648 return (String ) keyGenerator.generateKey(); 649 } 650 } | Popular Tags |