View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.event.handler;
23  
24  import org.opencastproject.message.broker.api.assetmanager.AssetManagerItem;
25  import org.opencastproject.message.broker.api.update.AssetManagerUpdateHandler;
26  
27  import org.osgi.service.component.ComponentContext;
28  import org.osgi.service.component.annotations.Activate;
29  import org.osgi.service.component.annotations.Component;
30  import org.osgi.service.component.annotations.Deactivate;
31  import org.osgi.service.component.annotations.Reference;
32  import org.osgi.service.component.annotations.ReferenceCardinality;
33  import org.osgi.service.component.annotations.ReferencePolicy;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * This handler listens for changes to episodes. Whenever a change is done, this is propagated to OAI-PMH.
39   */
40  @Component(
41      immediate = true,
42      service = {
43          AssetManagerUpdateHandler.class
44      },
45      property = {
46          "service.description=Conducting event handler for recording events",
47      }
48  )
49  public class ConductingEpisodeUpdatedEventHandler implements AssetManagerUpdateHandler {
50  
51    private static final Logger logger = LoggerFactory.getLogger(ConductingEpisodeUpdatedEventHandler.class);
52  
53    private OaiPmhUpdatedEventHandler oaiPmhUpdatedEventHandler;
54  
55    @Activate
56    public void activate(ComponentContext cc) {
57      logger.info("Activating {}", ConductingEpisodeUpdatedEventHandler.class.getName());
58    }
59  
60    @Deactivate
61    public void deactivate(ComponentContext cc) {
62      logger.info("Deactivating {}", ConductingEpisodeUpdatedEventHandler.class.getName());
63    }
64  
65    @Override
66    public void execute(AssetManagerItem messageItem) {
67      if (! (messageItem instanceof AssetManagerItem.TakeSnapshot)) {
68        // We don't want to handle anything but TakeSnapshot messages.
69        return;
70      }
71      AssetManagerItem.TakeSnapshot snapshotItem = (AssetManagerItem.TakeSnapshot) messageItem;
72      if (AssetManagerItem.Type.Update.equals(snapshotItem.getType())) {
73        // the OAI-PMH handler is a dynamic dependency
74        if (oaiPmhUpdatedEventHandler != null) {
75          oaiPmhUpdatedEventHandler.handleEvent(snapshotItem);
76        }
77      }
78    }
79  
80    /**
81     * OSGi DI callback.
82     */
83    @Reference (
84        policy = ReferencePolicy.DYNAMIC,
85        cardinality = ReferenceCardinality.OPTIONAL,
86        unbind = "unsetOaiPmhUpdatedEventHandler"
87    )
88    public void setOaiPmhUpdatedEventHandler(OaiPmhUpdatedEventHandler h) {
89      this.oaiPmhUpdatedEventHandler = h;
90    }
91  
92    public void unsetOaiPmhUpdatedEventHandler(OaiPmhUpdatedEventHandler h) {
93      if (this.oaiPmhUpdatedEventHandler == h) {
94        this.oaiPmhUpdatedEventHandler = null;
95      }
96    }
97  }