View Javadoc
1   package net.sf.logdistiller;
2   
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  import java.io.*;
18  import java.util.*;
19  
20  public class PublishHelper
21  {
22      private final LogDistillation ld;
23  
24      private final List<String> publishers;
25  
26      public PublishHelper( LogDistillation ld )
27      {
28          this( ld, new ArrayList<String>() );
29      }
30  
31      public PublishHelper( LogDistillation ld, List<String> publishers )
32      {
33          this.ld = ld;
34          this.publishers = publishers;
35      }
36  
37      public int publish()
38          throws IOException, Publisher.PublishException
39      {
40          int published = publishGlobal();
41  
42          LogDistillation.Group[] groups = ld.getGroups();
43          int count = groups.length;
44          for ( int i = 0; i < count; i++ )
45          {
46              LogDistillation.Group group = groups[i];
47              if ( ( !group.isSkip() ) && ( group.getEventCount() > 0 ) )
48              {
49                  published += publishGroup( group );
50              }
51          }
52          return published;
53      }
54  
55      public int publishGlobal()
56          throws IOException, Publisher.PublishException
57      {
58          LogDistiller.Report[] reports = ld.getDefinition().getOutput().getReports();
59          int count = reports.length;
60          int published = 0;
61          for ( int i = 0; i < count; i++ )
62          {
63              LogDistiller.Report report = reports[i];
64              Publisher publisher = Publishers.getPublisher( report.getPublisher() );
65              if ( isPublisherEnabled( publisher ) )
66              {
67                  publisher.publish( ld, report );
68                  published++;
69              }
70          }
71          return published;
72      }
73  
74      public int publishGroup( LogDistillation.Group group )
75          throws IOException, Publisher.PublishException
76      {
77          LogDistiller.Report[] reports = group.getDefinition().getReports();
78          int count = reports.length;
79          int published = 0;
80          for ( int i = 0; i < count; i++ )
81          {
82              LogDistiller.Report report = reports[i];
83              Publisher publisher = Publishers.getPublisher( report.getPublisher() );
84              if ( isPublisherEnabled( publisher ) )
85              {
86                  publisher.publish( group, report );
87                  published++;
88              }
89          }
90          return published;
91      }
92  
93      private boolean isPublisherEnabled( Publisher publisher )
94      {
95          return ( publisher != null ) && ( ( publishers == null ) || publishers.contains( publisher.getId() ) );
96      }
97  }