1 25 27 package de.nava.informa.utils; 28 29 import java.util.*; 30 import java.util.List ; 31 32 import net.sf.hibernate.*; 33 import net.sf.hibernate.Hibernate; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import de.nava.informa.core.ChannelBuilderException; 39 import de.nava.informa.impl.hibernate.*; 40 import de.nava.informa.impl.hibernate.ChannelBuilder; 41 42 49 public class PersistChanGrpMgr 50 { 51 52 private static final int DEFAULT_STARTDELAY = 1 * 1000; private static final int DEFAULT_PERIOD = 10 * 60 * 1000; private static final int DEFAULT_ACCEPTERRORS = 10; 55 56 private static final int DBG_DEFAULT_STARTDELAY = 100; private static final int DBG_DEFAULT_PERIOD = 20000; private static final int DBG_DEFAULT_ACCEPTERRORS = 10; 59 60 private static Log logger = LogFactory.getLog(PersistChanGrpMgr.class); 61 private ChannelBuilder builder; 62 private ChannelGroup group; 63 private SessionHandler handler; 64 private PersistChanGrpMgrObserverIF globalChannelObserver; 65 private boolean activated = false; 66 private PersistChanGrpMgrTask task; 67 private int pollingCounter; 68 69 int taskStartDelay; 70 int taskPeriod; 71 int acceptNrErrors; 72 73 81 public PersistChanGrpMgr(SessionHandler handler, boolean debug) 82 { 83 if (handler == null) throw new IllegalStateException ("Invalid handler"); 84 this.handler = handler; 85 builder = new ChannelBuilder(handler); 86 pollingCounter = 0; 87 88 if (debug) 89 { 90 taskStartDelay = DBG_DEFAULT_STARTDELAY; 91 taskPeriod = DBG_DEFAULT_PERIOD; 92 acceptNrErrors = DBG_DEFAULT_ACCEPTERRORS; 93 } else 94 { 95 taskStartDelay = DEFAULT_STARTDELAY; 96 taskPeriod = DEFAULT_PERIOD; 97 acceptNrErrors = DEFAULT_ACCEPTERRORS; 98 } 99 } 100 101 107 public ChannelGroup createGroup(String name) 108 { 109 logger.debug("Creating Persistent Group: " + name); 110 if (group != null) throw new IllegalStateException ("Can't call createGroup twice in a row."); 111 if (activated) throw new IllegalStateException ("Can't create groups while activated."); 112 113 ChannelGroup result = null; 114 synchronized (builder) 115 { 116 result = findChannelGroup(name); 117 if (result == null) 118 { 119 try 120 { 121 builder.beginTransaction(); 122 result = (ChannelGroup) builder.createChannelGroup(name); 123 builder.endTransaction(); 124 } catch (ChannelBuilderException e) 125 { 126 try 127 { 128 builder.endTransaction(); 129 } catch (ChannelBuilderException e1) 130 { 131 e1.printStackTrace(); 132 } 133 e.printStackTrace(); 134 } 135 } 136 group = result; 137 } 138 logger.info("createGroup(\"" + name + "\" yielded: " + result); 139 return result; 140 } 141 142 145 public void deleteGroup() 146 { 147 if (group == null) return; 148 logger.debug("Deleting Persistent Group: " + group.getTitle()); 149 150 synchronized (builder) 151 { 152 try 153 { 154 builder.beginTransaction(); 155 builder.reload(group); 156 157 Channel[] chans = (Channel[])group.getChannels().toArray(new Channel[0]); 159 for (int i = 0; i < chans.length; i++) 160 { 161 Channel chan = chans[i]; 162 163 final Set grps = chan.getGroups(); 164 grps.remove(group); 165 group.getChannels().remove(chan); 166 167 if (grps.size() == 0) builder.delete(chan); 169 } 170 171 builder.delete(group); 172 173 builder.endTransaction(); 174 group = null; 175 } catch (ChannelBuilderException e) 176 { 177 logger.error("Unable to delete Persistent Group: " + e.getMessage()); 178 builder.resetTransaction(); 179 } 180 } 181 } 182 183 189 public boolean hasChannel(final Channel achannel) 190 { 191 return group.getChannels().contains(achannel); 192 } 193 194 201 public Channel addChannel(String url) 202 { 203 if (activated) throw new IllegalStateException ("can't add Channels while activated."); 204 Channel achannel = null; 205 synchronized (builder) 206 { 207 try 208 { 209 builder.beginTransaction(); 210 builder.reload(group); 211 212 achannel = findChannel(url); 213 if (achannel == null) 214 { achannel = newChannel(url); 216 logger.debug("Added New Channel: " + url); 217 } else 218 { if (!hasChannel(achannel)) 220 { 221 logger.debug("Loaded existing channel" + url); 222 group.add(achannel); 223 achannel.getGroups().add(group); 224 } 225 } 226 builder.endTransaction(); 227 } catch (Exception e) 228 { 229 e.printStackTrace(); 230 builder.resetTransaction(); 231 } 232 return achannel; 233 } 234 } 235 236 242 public void moveChannelTo(Channel channel, PersistChanGrpMgr destGrp) 243 { 244 if (activated || destGrp.isActivated()) 245 throw new IllegalStateException ("can't move Channels while activated."); 246 synchronized (builder) 247 { 248 try 249 { 250 builder.beginTransaction(); 251 builder.reload(group); 252 builder.reload(channel); 253 ChannelGroup dstGroup = builder.reload(destGrp.getChannelGroup()); 254 255 group.remove(channel); 256 channel.getGroups().remove(group); 257 258 dstGroup.add(channel); 259 channel.getGroups().add(dstGroup); 260 261 builder.endTransaction(); 262 } catch (Exception e) 263 { 264 e.printStackTrace(); 265 builder.resetTransaction(); 266 } 267 } 268 } 269 270 277 public boolean deleteChannel(Channel channel) 278 { 279 boolean result = false; 280 if (activated) throw new IllegalStateException ("can't delete Channels while activated."); 281 synchronized (builder) 282 { 283 try 284 { 285 builder.beginTransaction(); 286 builder.reload(group); 287 builder.reload(channel); 288 289 if (hasChannel(channel)) 290 { 291 group.remove(channel); 292 channel.getGroups().remove(group); 293 builder.delete(channel); 294 295 result = true; 296 } 297 298 builder.endTransaction(); 299 } catch (Exception e) 300 { 301 e.printStackTrace(); 302 builder.resetTransaction(); 303 } 304 } 305 306 return result; 307 } 308 309 317 public int deleteItemFromChannel(Channel channel, Item item) 318 { 319 if (activated) throw new IllegalStateException ("can't delete Items while activated"); 320 int result = 0; 321 synchronized (builder) 322 { 323 try 324 { 325 builder.beginTransaction(); 326 builder.reload(channel); 327 builder.reload(item); 328 329 channel.removeItem(item); 330 builder.delete(item); 331 332 result = channel.getItems().size(); 333 334 builder.endTransaction(); 335 } catch (ChannelBuilderException e) 336 { 337 e.printStackTrace(); 338 builder.resetTransaction(); 339 } 340 } 341 return result; 342 } 343 344 350 public int getItemCount(Channel channel) { 351 if (activated) throw new IllegalStateException ("can't count Items while activated"); 352 int result = 0; 353 synchronized (builder) 354 { 355 try 356 { 357 builder.beginTransaction(); 358 builder.reload(channel); 359 360 result = channel.getItems().size(); 361 362 builder.endTransaction(); 363 } catch (ChannelBuilderException e) 364 { 365 e.printStackTrace(); 366 builder.resetTransaction(); 367 } 368 } 369 return result; 370 } 371 379 380 387 public void notifyChannelsAndItems(Channel channel) 388 { 389 synchronized (builder) 390 { 391 try 392 { 393 builder.beginTransaction(); 394 builder.reload(channel); 395 396 notifyChannelRetrieved(channel); 397 notifyItems(channel); 398 399 builder.endTransaction(); 400 } catch (ChannelBuilderException e) 401 { 402 e.printStackTrace(); 403 builder.resetTransaction(); 404 } 405 } 406 } 407 408 413 public void notifyItems(Channel channelHandle) 414 { 415 if (globalChannelObserver != null) 416 { 417 Iterator iterChan = channelHandle.getItems().iterator(); 418 while (iterChan.hasNext()) 419 { 420 notifyItemAdded((Item) iterChan.next()); 421 } 422 } 423 } 424 425 429 public void notifyChannelsAndItems() 430 { 431 Iterator chanIter = group.getChannels().iterator(); 432 while (chanIter.hasNext()) 433 { 434 notifyChannelsAndItems((Channel) chanIter.next()); 435 } 436 } 437 438 441 public void notifyChannels() 442 { 443 Iterator chanIter = channelIterator(); 445 while (chanIter.hasNext()) 446 { 447 notifyChannelRetrieved((Channel) chanIter.next()); 448 } 449 } 450 451 456 public void notifyChannelRetrieved(Channel chan) 457 { 458 if (globalChannelObserver != null) 459 { 460 try 461 { 462 globalChannelObserver.channelRetrieved(chan); 463 } catch (Exception e) 464 { 465 logger.error(e.getMessage(), e); 467 } 468 } 469 } 470 471 476 public void notifyItemAdded(Item newItem) 477 { 478 if (globalChannelObserver != null) 479 { 480 try 481 { 482 globalChannelObserver.itemAdded(newItem); 483 } catch (Exception e) 484 { 485 logger.error(e.getMessage(), e); 487 } 488 } 489 } 490 491 496 public void notifyPolling(boolean isPolling) 497 { 498 if (globalChannelObserver != null) 499 { 500 try 501 { 502 globalChannelObserver.pollingNow(group.getTitle(), pollingCounter, isPolling); 503 } catch (Exception e) 504 { 505 logger.error(e.getMessage(), e); 507 } 508 } 509 } 510 511 517 public void setGlobalObserver(PersistChanGrpMgrObserverIF obser) 518 { 519 globalChannelObserver = obser; 520 } 521 522 526 public synchronized void activate() 527 { 528 if (activated) return; 529 530 task = new PersistChanGrpMgrTask(this, taskPeriod); 531 task.start(); 532 533 activated = true; 534 } 535 536 543 public boolean isActivated() 544 { 545 return activated || (task != null && task.isRunning()); 546 } 547 548 552 public void incrPollingCounter() 553 { 554 pollingCounter++; 555 } 556 557 562 public int getPollingCounter() 563 { 564 return pollingCounter; 565 } 566 567 570 public synchronized void deActivate() 571 { 572 deActivate(false); 573 } 574 575 581 public synchronized void deActivate(final boolean waitForFinish) 582 { 583 if (task != null) 584 logger.debug("deActivate(" + task.getName() + ") " + activated); 585 else 586 logger.debug("deActivate task = null"); 587 588 if (!activated) return; 589 590 task.interrupt(waitForFinish); 591 592 activated = false; 593 } 594 595 603 public void setParams(final int startDel, final int period, final int acceptErr) 604 { 605 if (activated) throw new IllegalStateException ("can't setParams while activated"); 606 if (startDel != -1) taskStartDelay = startDel; 607 if (period != -1) taskPeriod = period; 608 if (acceptErr != -1) acceptNrErrors = acceptErr; 609 } 610 611 615 public Iterator channelIterator() 616 { 617 Iterator ret = null; 618 synchronized (builder) 619 { 620 try 621 { 622 builder.beginTransaction(); 623 builder.reload(group); 624 625 ret = group.getAll().iterator(); 626 627 builder.endTransaction(); 628 } catch (Exception e) 629 { 630 e.printStackTrace(); 631 } 632 } 633 return ret; 634 } 635 636 641 public ChannelBuilder getBuilder() 642 { 643 return builder; 644 } 645 646 651 public ChannelGroup getChannelGroup() 652 { 653 return group; 654 } 655 656 661 public SessionHandler getHandler() 662 { 663 return handler; 664 } 665 666 669 public int getAcceptNrErrors() 670 { 671 return acceptNrErrors; 672 } 673 674 679 public String toString() 680 { 681 String result = ""; 682 synchronized (builder) 683 { 684 try 685 { 686 builder.beginTransaction(); 687 builder.reload(group); 688 689 result = group.getTitle() + "[" + group.getChannels().size() + "]"; 690 691 builder.endTransaction(); 692 } catch (Exception e) 693 { 694 e.printStackTrace(); 695 } 696 } 697 return result; 698 } 699 700 706 private Channel newChannel(String url) 707 { 708 Channel channel; 709 synchronized (builder) 710 { 711 channel = (Channel) builder.createChannel("[uninitialized channel]"); 712 } 713 channel.setLocationString(url); 714 715 group.add(channel); 716 channel.getGroups().add(group); 717 718 return channel; 719 } 720 721 729 private Channel findChannel(String url) 730 { 731 Channel achan = null; 732 synchronized (builder) 733 { 734 Session sess = builder.getSession(); 735 try 736 { 737 final List channels = sess.find("from Channel chan where chan.locationString = ? order by chan.id desc", 738 url, Hibernate.STRING); 739 740 742 final int size = channels.size(); 743 if (size > 0) 744 { 745 if (size > 1) 746 { 747 logger.error("Multiple Channels for " + url + " found."); 748 } 749 achan = (Channel) channels.get(0); 750 } 751 } catch (HibernateException e) 752 { 753 achan = null; 754 e.printStackTrace(); 755 } 756 } 757 758 logger.info("findChannel: " + url + "->" + achan); 759 return achan; 760 } 761 762 768 private ChannelGroup findChannelGroup(String name) 769 { 770 ChannelGroup result = null; 771 synchronized (builder) 772 { 773 try 774 { 775 builder.beginTransaction(); 776 777 final Session sess = builder.getSession(); 778 final List results = sess.find("from ChannelGroup as grp where grp.title = ?", 779 name, Hibernate.STRING); 780 781 final int size = results.size(); 782 if (size > 0) 783 { 784 if (size > 1) 785 { 786 logger.error("Multiple Channel Groups called " + name + " found."); 787 } 788 result = (ChannelGroup) results.get(0); 789 } 790 791 builder.endTransaction(); 792 } catch (Exception e) 793 { 794 e.printStackTrace(); 795 builder.resetTransaction(); 796 } 797 } 798 799 logger.info("findChannelGroup: " + name + "->" + result); 800 return result; 801 } 802 } 803 | Popular Tags |