首页 > 技术文章 > 模仿插件通知机制实现的serviceProvider

dingnate 2014-03-18 11:03 原文

扩展点文件:notifierProvider.exsd

<?xml version='1.0' encoding='UTF-8'?>
<schema targetNamespace="test" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
      <appInfo>
         <meta.schema plugin="test.plugin" id="notifierProvider" name="Notifier Service Provider"/>
      </appInfo>
      <documentation>
         [Enter description of this extension point.]
      </documentation>
   </annotation>

   <element name="extension">
      <annotation>
         <appInfo>
            <meta.element />
         </appInfo>
      </annotation>
      <complexType>
         <sequence>
            <element ref="NotifierProvider"/>
         </sequence>
         <attribute name="point" type="string" use="required">
            <annotation>
               <documentation>
                 
               </documentation>
            </annotation>
         </attribute>
         <attribute name="id" type="string">
            <annotation>
               <documentation>
                 
               </documentation>
            </annotation>
         </attribute>
         <attribute name="name" type="string">
            <annotation>
               <documentation>
                 
               </documentation>
               <appInfo>
                  <meta.attribute translatable="true"/>
               </appInfo>
            </annotation>
         </attribute>
      </complexType>
   </element>

   <element name="NotifierProvider">
      <complexType>
         <sequence>
            <element ref="Priority"/>
            <element ref="Event" minOccurs="1" maxOccurs="unbounded"/>
         </sequence>
         <attribute name="class" type="string" use="required">
            <annotation>
               <documentation>
                 
               </documentation>
               <appInfo>
                  <meta.attribute kind="java" basedOn="test.AbstractNoifierProvider:"/>
               </appInfo>
            </annotation>
         </attribute>
      </complexType>
   </element>

   <element name="Event">
      <complexType>
         <attribute name="eventId" type="string" use="required">
            <annotation>
               <documentation>
                 
               </documentation>
            </annotation>
         </attribute>
      </complexType>
   </element>

   <element name="Priority">
      <complexType>
         <attribute name="name" use="required">
            <annotation>
               <documentation>
                 
               </documentation>
            </annotation>
            <simpleType>
               <restriction base="string">
                  <enumeration value="Lowest">
                  </enumeration>
                  <enumeration value="Low">
                  </enumeration>
                  <enumeration value="Medium">
                  </enumeration>
                  <enumeration value="High">
                  </enumeration>
                  <enumeration value="Highest">
                  </enumeration>
               </restriction>
            </simpleType>
         </attribute>
      </complexType>
   </element>

   <annotation>
      <appInfo>
         <meta.section type="since"/>
      </appInfo>
      <documentation>
         [Enter the first release in which this extension point appears.]
      </documentation>
   </annotation>

   <annotation>
      <appInfo>
         <meta.section type="examples"/>
      </appInfo>
      <documentation>
         [Enter extension point usage example here.]
      </documentation>
   </annotation>

   <annotation>
      <appInfo>
         <meta.section type="apiinfo"/>
      </appInfo>
      <documentation>
         [Enter API information here.]
      </documentation>
   </annotation>

   <annotation>
      <appInfo>
         <meta.section type="implementation"/>
      </appInfo>
      <documentation>
         [Enter information about supplied implementation of this extension point.]
      </documentation>
   </annotation>


</schema>

 

通知器接口INotifier

/**
 * 通知器接口
 */
public interface INotifier
{
    /**
     * 通知事件
     * @param event
     */
    void notify(INotifierEvent event);
   
    /**
     * 获取事件id,也就是这个通知器能处理哪些事件
     * @return 事件id
     */
    String[] getEventIds();
}

 


扩展点抽象基类AbstractNoifierProvider

/**
 * 抽象的通知提供器
 */
public abstract class AbstractNoifierProvider extends AbstractProvider implements INotifier
{
    private List<String> eventIds = new ArrayList<String>();
   
    /*
     * (此注释不是Javadoc注释)
     *
     */
    @Override
    public String[] getEventIds()
    {
        return eventIds.toArray(new String[eventIds.size()]);
    }
   
    /**
     * 添加事件id
     *
     * @param eventId
     */
    void addEventId(String eventId)
    {
        if (!StringUtils.isEmpty(eventId))
        {
            eventIds.add(eventId);
        }
    }
   
    /*
     * (此注释不是Javadoc注释)
     *
     * @see
     * org.eclipse.gmf.runtime.common.core.service.IProvider#provides(org.eclipse.gmf.runtime.common.core.service.IOperation
     * )
     */
    @Override
    public boolean provides(IOperation operation)
    {
        if (operation instanceof NotifierOperation)
        {
            String eventID = ((NotifierOperation)operation).getNotifierID();
            if (StringUtils.isNotEmpty(eventID))
            {
                for (int i = 0; i < eventIds.size(); i++)
                {
                    if (eventID.equals(eventIds.get(i)))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
   
}

通知操作对象NotifierOperation
/**
 * 通知操作对象
 */
public class NotifierOperation implements IOperation
{
   
    private INotifierEvent event;
   
    /* (此注释不是Javadoc注释)
     * @see org.eclipse.gmf.runtime.common.core.service.IOperation#execute(org.eclipse.gmf.runtime.common.core.service.IProvider)
     */
    /**
     * 构造函数
     * @param event
     */
    public NotifierOperation(INotifierEvent event)
    {
        super();
        this.event = event;
    }
   
    @Override
    public Object execute(IProvider provider)
    {
        if (provider instanceof INotifier)
        {
            ((INotifier)provider).notify(event);
        }
        return null;
    }
   
    /**
     * 获取事件id
     * @return 事件id
     */
    public String getNotifierID()
    {
        if (event != null)
        {
            return event.getEventId();
        }
        return StringUtils.EMPTY;
    }
}


通知服务NotifierService

/**
 * 通知服务,针对事件的通知
 */
public class NotifierService extends Service implements INotifier
{
    private static NotifierService instance = new NotifierService();
   
    static
    {
        instance.configureProviders(TestPlugin.PLUGIN_ID,
                "notifierProvider"); //$NON-NLS-1$
    }
   
    /**
     * 构造函数
     */
    private NotifierService()
    {
        super();
    }
   
    /**
     * 获取单态对象
     * @return NotifierService
     */
    public static NotifierService getInstance()
    {
        return instance;
    }
   
    /* (此注释不是Javadoc注释)
     * @see com.huawei.enip.esdt.project.core.notifier.INotifier#getEventIds()
     */
    @SuppressWarnings("unchecked")
    @Override
    public String[] getEventIds()
    {
        List<String> allEvents = new ArrayList<String>();
        List<Object> allProviders = getAllProviders();
        if (allProviders == null || allProviders.isEmpty())
        {
            return allEvents.toArray(new String[allEvents.size()]);
        }
       
        for (int i = 0; i < allProviders.size(); i++)
        {
            Object object = allProviders.get(i);
            if (object instanceof AbstractNoifierProvider)
            {
                String[] eventIds = ((AbstractNoifierProvider)object).getEventIds();
                if (eventIds == null)
                {
                    continue;
                }
                for (int j = 0; j < eventIds.length; j++)
                {
                    if (!StringUtils.isEmpty(eventIds[j]))
                    {
                        allEvents.add(eventIds[j]);
                    }
                }
            }
        }
        return allEvents.toArray(new String[allEvents.size()]);
    }
   
    @Override
    public void notify(INotifierEvent event)
    {
        NotifierOperation operation = new NotifierOperation(event);
        execute(ExecutionStrategy.FORWARD, operation);
    }
   
    @Override
    protected ProviderDescriptor newProviderDescriptor(
            IConfigurationElement element)
    {
        //扩展的描述
        return new NotifierServiceProviderDescriptor(element);
    }
   
    /**
     * 通知服务扩展的描述
     */
    public static class NotifierServiceProviderDescriptor extends
            Service.ProviderDescriptor
    {
        private static final String Event = "Event"; //$NON-NLS-1$
       
        private static final String eventId = "eventId"; //$NON-NLS-1$
       
        /**
         * 构造函数
         * @param arg0
         */
        protected NotifierServiceProviderDescriptor(IConfigurationElement arg0)
        {
            super(arg0);
        }
       
        /* (此注释不是Javadoc注释)
         * @see org.eclipse.gmf.runtime.common.core.service.Service.ProviderDescriptor#getProvider()
         */
        @Override
        public IProvider getProvider()
        {
            IProvider iProvider = super.getProvider();
            if (iProvider instanceof AbstractNoifierProvider)
            {
                IConfigurationElement[] children = getElement().getChildren(Event);
                if (children != null)
                {
                    for (int i = 0; i < children.length; i++)
                    {
                        IConfigurationElement element = children[i];
                        String attribute = element.getAttribute(eventId);
                        ((AbstractNoifierProvider)iProvider).addEventId(attribute);
                    }
                }
            }
            return iProvider;
        }
    }
}

推荐阅读