/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Kafka.Client.ZooKeeperIntegration.Events { using System.Linq; using log4net; /// /// Represents methods that will handle a ZooKeeper data events /// internal class DataChangedEventItem { private readonly ILog logger; private ZooKeeperClient.ZooKeeperEventHandler dataChanged; private ZooKeeperClient.ZooKeeperEventHandler dataDeleted; /// /// Occurs when znode data changes /// public event ZooKeeperClient.ZooKeeperEventHandler DataChanged { add { this.dataChanged -= value; this.dataChanged += value; } remove { this.dataChanged -= value; } } /// /// Occurs when znode data deletes /// public event ZooKeeperClient.ZooKeeperEventHandler DataDeleted { add { this.dataDeleted -= value; this.dataDeleted += value; } remove { this.dataDeleted -= value; } } /// /// Initializes a new instance of the class. /// /// /// The logger. /// /// /// Should use external logger to keep same format of all event logs /// public DataChangedEventItem(ILog logger) { this.logger = logger; } /// /// Initializes a new instance of the class. /// /// /// The logger. /// /// /// The changed handler. /// /// /// The deleted handler. /// /// /// Should use external logger to keep same format of all event logs /// public DataChangedEventItem( ILog logger, ZooKeeperClient.ZooKeeperEventHandler changedHandler, ZooKeeperClient.ZooKeeperEventHandler deletedHandler) { this.logger = logger; this.DataChanged += changedHandler; this.DataDeleted += deletedHandler; } /// /// Invokes subscribed handlers for ZooKeeeper data changes event /// /// /// The event data. /// public void OnDataChanged(ZooKeeperDataChangedEventArgs e) { var handlers = this.dataChanged; if (handlers == null) { return; } foreach (var handler in handlers.GetInvocationList()) { this.logger.Debug(e + " sent to " + handler.Target); } handlers(e); } /// /// Invokes subscribed handlers for ZooKeeeper data deletes event /// /// /// The event data. /// public void OnDataDeleted(ZooKeeperDataChangedEventArgs e) { var handlers = this.dataDeleted; if (handlers == null) { return; } foreach (var handler in handlers.GetInvocationList()) { this.logger.Debug(e + " sent to " + handler.Target); } handlers(e); } /// /// Gets the total count of subscribed handlers /// public int TotalCount { get { return (this.dataChanged != null ? this.dataChanged.GetInvocationList().Count() : 0) + (this.dataDeleted != null ? this.dataDeleted.GetInvocationList().Count() : 0); } } } }