View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.identitystore.service.listeners;
35  
36  import fr.paris.lutece.plugins.identitystore.business.identity.Identity;
37  import fr.paris.lutece.plugins.identitystore.service.AttributeChangeListener;
38  import fr.paris.lutece.plugins.identitystore.service.IdentityChangeListener;
39  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.AttributeStatus;
40  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.common.RequestAuthor;
41  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.AttributeChangeType;
42  import fr.paris.lutece.plugins.identitystore.v3.web.rs.dto.history.IdentityChangeType;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  
47  import java.util.List;
48  import java.util.Map;
49  import java.util.concurrent.ExecutorService;
50  import java.util.concurrent.Executors;
51  
52  /**
53   *
54   */
55  public final class IdentityStoreNotifyListenerService
56  {
57      // singleton
58      private static IdentityStoreNotifyListenerService _singleton;
59  
60      // List
61      private final List<AttributeChangeListener> _attributeChangelistListeners;
62      private final List<IdentityChangeListener> _identityChangeListListeners;
63  
64      private final Integer poolSize = AppPropertiesService.getPropertyInt( "identitystore.listener.pool.size", 3 );
65  
66      private final ExecutorService attributeExecutor = Executors.newFixedThreadPool( poolSize );
67      private final ExecutorService identityExecutor = Executors.newFixedThreadPool( poolSize );
68  
69      /**
70       * private constructor
71       */
72      private IdentityStoreNotifyListenerService( )
73      {
74          // init attributeChangelistListeners
75          _attributeChangelistListeners = SpringContextService.getBeansOfType( AttributeChangeListener.class );
76  
77          StringBuilder sbLog = new StringBuilder( );
78          sbLog.append( "IdentityStore - loading listeners  : " );
79  
80          for ( final AttributeChangeListener listener : _attributeChangelistListeners )
81          {
82              sbLog.append( "\n\t\t\t\t - " ).append( listener.getName( ) );
83          }
84  
85          AppLogService.debug( sbLog.toString( ) );
86  
87          // init identityChangeListListeners
88          _identityChangeListListeners = SpringContextService.getBeansOfType( IdentityChangeListener.class );
89  
90          sbLog = new StringBuilder( );
91          sbLog.append( "IdentityStore - loading listeners  : " );
92  
93          for ( final IdentityChangeListener listener : _identityChangeListListeners )
94          {
95              sbLog.append( "\n\t\t\t\t - " ).append( listener.getName( ) );
96          }
97  
98          AppLogService.debug( sbLog.toString( ) );
99  
100     }
101 
102     /**
103      * Returns the unique instance
104      *
105      * @return The instance
106      */
107     public static IdentityStoreNotifyListenerService instance( )
108     {
109         if ( _singleton == null )
110         {
111             _singleton = new IdentityStoreNotifyListenerService( );
112         }
113         return _singleton;
114     }
115 
116     /**
117      * Notify an attribute change to all registered listeners
118      * 
119      * @param changeType
120      *            the type of change
121      * @param identity
122      *            the identity to which the attribute belongs
123      * @param attributeStatus
124      *            the attribute status
125      * @param author
126      *            the author of the change
127      * @param clientCode
128      *            the client code that triggered the change
129      */
130     public void notifyListenersAttributeChange( AttributeChangeType changeType, Identity identity, AttributeStatus attributeStatus, RequestAuthor author,
131             String clientCode )
132     {
133         _attributeChangelistListeners.stream( ).<Runnable> map( listener -> ( ) -> {
134             try
135             {
136                 listener.processAttributeChange( changeType, identity, attributeStatus, author, clientCode );
137             }
138             catch( Exception e )
139             {
140                 AppLogService.error( "An error occurred when notifying listener " + listener.getName( ) + " : " + e.getMessage( ) );
141             }
142         } ).forEach( attributeExecutor::submit );
143     }
144 
145     /**
146      * Notify an identityChange to all registered listeners
147      * 
148      * @param identityChangeType
149      *            the type of change
150      * @param identity
151      *            the identity that changed
152      * @param statusCode
153      *            the status code of the change
154      * @param statusMessage
155      *            the message
156      * @param author
157      *            the author of the change
158      * @param clientCode
159      *            the client code that triggered the change
160      * @param metadata
161      *            additional data
162      */
163     public void notifyListenersIdentityChange( IdentityChangeType identityChangeType, Identity identity, String statusCode, String statusMessage,
164             RequestAuthor author, String clientCode, Map<String, String> metadata )
165     {
166         _identityChangeListListeners.stream( ).<Runnable> map( listener -> ( ) -> {
167             try
168             {
169                 listener.processIdentityChange( identityChangeType, identity, statusCode, statusMessage, author, clientCode, metadata );
170             }
171             catch( Exception e )
172             {
173                 AppLogService.error( "An error occurred when notifying listener " + listener.getName( ) + " : " + e.getMessage( ) );
174             }
175         } ).forEach( identityExecutor::submit );
176     }
177 
178 }