-
Notifications
You must be signed in to change notification settings - Fork 121
/
ServiceProviderPlace.java
executable file
·1212 lines (1079 loc) · 43 KB
/
ServiceProviderPlace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package emissary.place;
import emissary.config.ConfigEntry;
import emissary.config.ConfigUtil;
import emissary.config.Configurator;
import emissary.core.EmissaryException;
import emissary.core.Family;
import emissary.core.Form;
import emissary.core.HDMobileAgent;
import emissary.core.IBaseDataObject;
import emissary.core.MobileAgent;
import emissary.core.Namespace;
import emissary.core.NamespaceException;
import emissary.core.ResourceException;
import emissary.core.ResourceWatcher;
import emissary.directory.DirectoryEntry;
import emissary.directory.DirectoryPlace;
import emissary.directory.EmissaryNode;
import emissary.directory.IDirectoryPlace;
import emissary.directory.KeyManipulator;
import emissary.directory.WildcardEntry;
import emissary.kff.KffDataObjectHandler;
import emissary.log.MDCConstants;
import emissary.parser.SessionParser;
import emissary.server.EmissaryServer;
import emissary.server.mvc.adapters.DirectoryAdapter;
import emissary.util.JMXUtil;
import com.codahale.metrics.Timer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import static emissary.core.constants.Configurations.PLACE_NAME;
import static emissary.core.constants.Configurations.PLACE_RESOURCE_LIMIT_MILLIS;
import static emissary.core.constants.Configurations.SERVICE_COST;
import static emissary.core.constants.Configurations.SERVICE_DESCRIPTION;
import static emissary.core.constants.Configurations.SERVICE_KEY;
import static emissary.core.constants.Configurations.SERVICE_NAME;
import static emissary.core.constants.Configurations.SERVICE_PROXY;
import static emissary.core.constants.Configurations.SERVICE_PROXY_DENY;
import static emissary.core.constants.Configurations.SERVICE_QUALITY;
import static emissary.core.constants.Configurations.SERVICE_TYPE;
/**
* Concrete instances of ServiceProviderPlace can be created by the emissary.admin.PlaceStarter and registered with the
* emissary.directory.IDirectoryPlace to make their respective services available and a specified cost and quality
* throughout the system.
*/
public abstract class ServiceProviderPlace implements IServiceProviderPlace,
ServiceProviderPlaceMBean {
/**
* Container for all configuration parameters read from the configuration file for this place. The net result is that
* many name value pairs are loaded from one or more files. See ServiceConfigGuide for details.
*
* @see emissary.config.ServiceConfigGuide
*/
@Nullable
protected Configurator configG;
/**
* A <i><b>local</b></i> reference to the directory that this place resides in. Every JVM that contains 'places' must
* have a local directory
*
* @see emissary.directory.DirectoryPlace
*/
@Nullable
protected String dirPlace;
@Nullable
protected IDirectoryPlace localDirPlace = null;
/**
* set of keys for this place read from configG. Each of the values defined by
* SERVICE_PROXY.SERCVICE_TYPE.SERVICE_NAME.PLACE_LOCATION$EXPENSE from the config file or KEY values from the config
* file.
*/
protected List<String> keys = new ArrayList<>();
/**
* List of denied places in SERVICE_PROXY_DENY
*/
protected List<String> denyList = new ArrayList<>();
// Items that are going to be deprecated, but here now to
// make the transition easier, for compatibility
@Nullable
protected String myKey = null;
protected int serviceCost = -1;
protected int serviceQuality = -1;
@Nullable
protected String placeName = null;
/**
* Text description of what the place does, usually from config file
*/
@Nullable
protected String serviceDescription;
/**
* Static context logger
*/
protected static final Logger slogger = LoggerFactory.getLogger(ServiceProviderPlace.class);
/**
* Dynamic context logger uses run-time classname as category
*/
protected Logger logger;
/**
* Set up handler for rehashing
*/
@Nullable
protected KffDataObjectHandler kff = null;
private static final String DOT = ".";
private static final String UNUSED_PROXY = "UNUSABLE-XyZZy";
/**
* These are used to track process vs processHD implementations to know whether one can proxy for the other one
*/
protected boolean processMethodImplemented = false;
protected boolean heavyDutyMethodImplemented = false;
/**
* Create a place and register it in the local directory. The default config must contain at least one SERVICE_KEY
* element used to know where that is and how to name it. If the old style config with SERVICE_PROXY etc is used then
* the PlaceName becomes the runtime class name of the instance without the package.
*/
public ServiceProviderPlace() throws IOException {
super();
String placeLocation = this.getClass().getSimpleName();
configG = loadConfigurator(placeLocation);
setupPlace(null, placeLocation);
}
/**
* Create a place and register it at the location specified. The location key contains the name of the place which is
* used to configure it. The local directory instance is found in the local Namespace.
*
* @param thePlaceLocation string name of our location
*/
public ServiceProviderPlace(String thePlaceLocation) throws IOException {
super();
configG = loadConfigurator(thePlaceLocation);
setupPlace(null, thePlaceLocation);
}
/**
* Create the place the normal way
*
* @param configFile file name for config data
* @param theDir string name of our directory
* @param thePlaceLocation string name of our location
*/
protected ServiceProviderPlace(String configFile, @Nullable String theDir, String thePlaceLocation) throws IOException {
super();
configG = loadConfigurator(configFile, thePlaceLocation);
setupPlace(theDir, thePlaceLocation);
}
/**
* Construct with config data from a stream
*
* @param configStream stream of config data
* @param theDir string name of our directory
* @param thePlaceLocation string name of our location
*/
protected ServiceProviderPlace(InputStream configStream, @Nullable String theDir, String thePlaceLocation) throws IOException {
super();
configG = loadConfigurator(configStream, thePlaceLocation);
setupPlace(theDir, thePlaceLocation);
}
/**
* Construct with config data from a stream on the local directory
*
* @param configStream stream of config data
*/
protected ServiceProviderPlace(InputStream configStream) throws IOException {
super();
String placeLocation = this.getClass().getSimpleName();
configG = loadConfigurator(configStream, placeLocation);
setupPlace(null, placeLocation);
}
/**
* Create the place with no directory
*
* @param configFile string name of config data file
* @param placeLocation string name of our location
*/
protected ServiceProviderPlace(String configFile, String placeLocation) throws IOException {
this(configFile, null, placeLocation);
}
/**
* Create the place with no directory
*
* @param configStream stream of config data
* @param placeLocation string name of our location
*/
protected ServiceProviderPlace(InputStream configStream, String placeLocation) throws IOException {
this(configStream, null, placeLocation);
}
/**
* Load the configurator
*
* @param configStream the stream to use or null to auto configure
*/
protected Configurator loadConfigurator(@Nullable InputStream configStream, String placeLocation) throws IOException {
// Read the configuration stream
if (configStream != null) {
// Use supplied stream
return ConfigUtil.getConfigInfo(configStream);
}
return loadConfigurator(placeLocation);
}
/**
* Load the configurator
*
* @param configFileName the file name to use or null to auto configure
*/
protected Configurator loadConfigurator(@Nullable String configFileName, String placeLocation) throws IOException {
// Read the configuration stream
if (configFileName != null) {
// Use supplied stream
return ConfigUtil.getConfigInfo(configFileName);
}
return loadConfigurator(placeLocation);
}
/**
* Load the configurator, figuring out whence automatically
*/
protected Configurator loadConfigurator(@Nullable String placeLocation) throws IOException {
if (placeLocation == null) {
placeLocation = this.getClass().getSimpleName();
}
// Extract config data stream name from place location
// and try finding config info with and without the
// package name of this class (in that order)
String myPackage = this.getClass().getPackage().getName();
List<String> configLocs = new ArrayList<>();
// Dont use KeyManipulator for this, only works when hostname/fqdn has dots
int pos = placeLocation.lastIndexOf("/");
String serviceClass = (pos > -1 ? placeLocation.substring(pos + 1) : placeLocation);
configLocs.add(myPackage + DOT + serviceClass + ConfigUtil.CONFIG_FILE_ENDING);
configLocs.add(serviceClass + ConfigUtil.CONFIG_FILE_ENDING);
return ConfigUtil.getConfigInfo(configLocs);
}
/**
* Help the constructor get the place running
*
* @param theDir name of our directory
*/
protected void setupPlace(@Nullable String theDir, String placeLocation) throws IOException {
// Customize the logger to the runtime class
logger = LoggerFactory.getLogger(this.getClass());
// The order of the following initialization calls
// is touchy. NPE all over if you mess up here.
// Set ServicePlace config items
configureServicePlace(placeLocation);
// Backwards compatibility setup items
DirectoryEntry firstentry = new DirectoryEntry(keys.get(0));
myKey = firstentry.getKey();
serviceCost = firstentry.getCost();
serviceQuality = firstentry.getQuality();
placeName = firstentry.getServiceLocation();
// configure directory references
if (!(this instanceof DirectoryPlace)) {
localizeDirectory(theDir);
logger.debug("Our localizedDirectory is {}", dirPlace);
} else {
logger.debug("Not localizing directory since we are a directory");
}
// Set up kff if we need it
if (this instanceof RehashingPlace || this instanceof MultiFileServerPlace) {
initKff();
}
// Bind to the namespace before registering
// our keys. This allows incoming traffic to find
// us as soon as they see the keys
for (String key : keys) {
String bindKey = KeyManipulator.getServiceLocation(key);
logger.debug("Binding myself into the namespace as {}", bindKey);
Namespace.bind(bindKey, this);
}
// Register with the directory
// This pushes all our keys out to the directory which
// sends them on in turn to peers, &c. in the p2p network
register();
// register MBean with JMX
JMXUtil.registerMBean(this);
// Verify and warn of incorrect process/processHeavyDuty implementation
verifyProcessImplementationProvided();
}
/**
* Get a local reference to the directory.
*
* @param theDir key for the directory to use, if null will look up default name
* @return true if it worked
*/
private boolean localizeDirectory(@Nullable String theDir) {
// Get a local (non proxy) copy of the directory if possible!
// Looking up both if nothing is provided
if (theDir == null) {
try {
localDirPlace = DirectoryPlace.lookup();
dirPlace = localDirPlace.toString();
} catch (EmissaryException ex) {
if (EmissaryServer.exists() && !(this instanceof DirectoryPlace)) {
logger.warn("Unable to find DirectoryPlace in local namespace", ex);
return false;
}
}
} else {
dirPlace = theDir;
localDirPlace = null;
try {
String myUrl = KeyManipulator.getServiceHostUrl(keys.get(0));
String dirUrl = KeyManipulator.getServiceHostUrl(dirPlace);
if (StringUtils.equals(dirUrl, myUrl)) {
localDirPlace = (IDirectoryPlace) Namespace.lookup(KeyManipulator.getServiceLocation(theDir));
} else {
logger.debug("Not localizing directory since dirPlace {} is not equal to myUrl {}", dirPlace, myUrl);
}
} catch (EmissaryException ex) {
logger.error("Exception attempting to get local reference to directory", ex);
return false;
}
}
return true;
}
/**
* Initialize the Kff Handler with our policy settings
*/
protected synchronized void initKff() {
kff =
new KffDataObjectHandler(KffDataObjectHandler.TRUNCATE_KNOWN_DATA, KffDataObjectHandler.SET_FORM_WHEN_KNOWN,
KffDataObjectHandler.SET_FILE_TYPE);
}
/**
* Set the logger to use, allows easier mocking among other things
*
* @param l the logger instance to use
*/
public void setLogger(Logger l) {
this.logger = l;
}
/**
* Return an encapsulation of our key and cost structure Only good for the top key on the list
*
* @return a DirectoryEntry for this place
*/
@Override
public DirectoryEntry getDirectoryEntry() {
return new DirectoryEntry(keys.get(0), serviceDescription, serviceCost, serviceQuality);
}
/**
* Configuration items read here are:
*
* <ul>
* <li>PLACE_NAME: place name portion of key, required</li>
* <li>SERVICE_NAME: service name portion of key, required</li>
* <li>SERVICE_TYPE: service type portion of key, required</li>
* <li>SERVICE_DESCRIPTION: description of place, required</li>
* <li>SERVICE_COST: cost of service provided, required</li>
* <li>SERVICE_QUALITY: quality of service provided, required</li>
* <li>SERVICE_PROXY: list of service proxy types for key</li>
* <li>SERVICE_KEY: full 4 part keys with expense</li>
* </ul>
*
* @param placeLocation the specified placeLocation or a full four part key to register with
*/
protected void configureServicePlace(@Nullable String placeLocation) throws IOException {
serviceDescription = configG.findStringEntry(SERVICE_DESCRIPTION);
if (serviceDescription == null || serviceDescription.length() == 0) {
serviceDescription = "Description not available";
}
if (placeLocation == null) {
placeLocation = this.getClass().getSimpleName();
}
String locationPart = placeLocation;
if (KeyManipulator.isKeyComplete(placeLocation)) {
keys.add(placeLocation); // save as first in list
locationPart = KeyManipulator.getServiceLocation(placeLocation);
} else if (!placeLocation.contains("://")) {
EmissaryNode node = new EmissaryNode();
locationPart = "http://" + node.getNodeName() + ":" + node.getNodePort() + "/" + placeLocation;
}
// Build keys the old fashioned way from parts specified in the config
String placeName = configG.findStringEntry(PLACE_NAME);
String serviceName = configG.findStringEntry(SERVICE_NAME);
String serviceType = configG.findStringEntry(SERVICE_TYPE);
int serviceCost = configG.findIntEntry(SERVICE_COST, -1);
int serviceQuality = configG.findIntEntry(SERVICE_QUALITY, -1);
// Bah.
if (placeName != null && placeName.length() == 0) {
placeName = null;
}
if (serviceName != null && serviceName.length() == 0) {
serviceName = null;
}
if (serviceType != null && serviceType.length() == 0) {
serviceType = null;
}
if (placeName != null && serviceName != null && serviceType != null && serviceCost > -1 && serviceQuality > -1) {
// pick up the proxies(save full 4-tuple keys!)
for (String sp : configG.findEntries(SERVICE_PROXY)) {
DirectoryEntry de = new DirectoryEntry(sp, serviceName, serviceType, locationPart, serviceDescription, serviceCost, serviceQuality);
keys.add(de.getFullKey());
}
// pick up the denied proxies(save full 4-tuple keys!)
for (String sp : configG.findEntries(SERVICE_PROXY_DENY)) {
DirectoryEntry de = new DirectoryEntry(sp, serviceName, serviceType, locationPart, serviceDescription, serviceCost, serviceQuality);
denyList.add(de.getDataType());
}
} else {
// May be configured the new way, but warn if there is a mixture of
// null and non-null items using the old-fashioned way. Perhaps the
// user just missed one of them
int nullCount = 0;
if (placeName == null) {
nullCount++;
}
if (serviceName == null) {
nullCount++;
}
if (serviceType == null) {
nullCount++;
}
if (serviceCost == -1) {
nullCount++;
}
if (serviceQuality == -1) {
nullCount++;
}
if (nullCount > 0 && nullCount < 5) {
throw new IOException(
"Missing configuration items. Please check SERVICE_NAME, SERVICE_TYPE, PLACE_NAME, SERVICE_COST, SERVICE_QUALITY");
}
}
// Now build any keys the new way
for (String k : configG.findEntries(SERVICE_KEY)) {
if (KeyManipulator.isKeyComplete(k)) {
keys.add(k);
} else {
logger.warn("SERVICE_KEY '{}' is missing parts and cannot be used", k);
}
}
// Make sure some keys were defined one way or the other
if (keys.isEmpty()) {
throw new IOException("NO keys were defined. Please configure at least one "
+ "SERVICE_KEY or SERVICE_NAME/SERVICE_TYPE/SERVICE_PROXY group");
}
}
/**
* Delegate nextKey to our directory
*
* @param dataId key to entryMap in directory, dataType::serviceType
* @param lastEntry place agent visited last, this is not stateless
* @return List of DirectoryEntry with next places to go
*/
@Override
@Nullable
public List<DirectoryEntry> nextKeys(final String dataId, final IBaseDataObject payload, final DirectoryEntry lastEntry) {
if (localDirPlace != null) {
return localDirPlace.nextKeys(dataId, payload, lastEntry);
}
logger.error("No local directory in place {} with dir={}", keys.get(0), dirPlace);
return null;
}
/**
* The ServiceProviderPlace facade for visiting agents
*
* @param payload dataobject from a MobileAgent
*/
@Override
public void agentProcessCall(IBaseDataObject payload) throws ResourceException {
try {
process(payload);
rehash(payload);
} catch (ResourceException r) {
throw r;
} catch (Exception e) {
logger.error("Place.process exception", e);
}
}
/**
* "HD" agent calls this method when visiting the place. If you use emissary.core.MobileAgent this method is never
* called. Should be overridden by concrete places that wish to process bulk data in a different manner than one payload
* at a time.
*
* @param payloadList list of IBaseDataObject from an HDMobileAgent
* @return list of IBaseDataObject "sprouts"
*/
@Override
public List<IBaseDataObject> agentProcessHeavyDuty(List<IBaseDataObject> payloadList) throws Exception {
logger.debug("Entering agentProcessHeavyDuty with {} payload items", payloadList.size());
List<IBaseDataObject> list = new ArrayList<>();
// For each incoming payload object
for (IBaseDataObject dataObject : payloadList) {
try {
// Process the payload item
List<IBaseDataObject> l = agentProcessHeavyDuty(dataObject);
if (!l.isEmpty()) {
dataObject.setNumChildren(dataObject.getNumChildren() + l.size());
}
// Accumulate results in a list to return
list.addAll(l);
} catch (Exception e) {
logger.error("Place.process exception", e);
dataObject.addProcessingError("agentProcessHD(" + keys.get(0) + "): " + e);
dataObject.replaceCurrentForm(Form.ERROR);
}
}
// Some debug output
if (logger.isDebugEnabled()) {
for (IBaseDataObject d : list) {
logger.debug("Returning child {} -> {}", d.shortName(), d.getAllCurrentForms());
}
}
return list;
}
/**
* "HD" method called only by HDMobileAgent for a single incoming payload
*
* @param payload data object to process
* @return List of IBaseDataObject "sprouts"
*/
@Override
public List<IBaseDataObject> agentProcessHeavyDuty(IBaseDataObject payload) throws Exception {
MDC.put(MDCConstants.SHORT_NAME, payload.shortName());
MDC.put(MDCConstants.SERVICE_LOCATION, this.getKey());
try {
List<IBaseDataObject> l = processHeavyDuty(payload);
payload.checkForUnsafeDataChanges();
rehash(payload);
return l;
} catch (Exception e) {
logger.error("Place.process threw:", e);
throw e;
}
}
/**
* Rehash the payload if this is a rehashing place
*
* @param payload the payload to evaluate and rehash
*/
protected void rehash(@Nullable IBaseDataObject payload) {
// Recompute hash if marker interface is enabled
if (this instanceof RehashingPlace && kff != null && payload != null) {
kff.hash(payload);
payload.setParameter(SessionParser.ORIG_DOC_SIZE_KEY, "" + payload.dataLength());
}
}
/**
* Convenience method to process a single payload when there is no expecation of decomposing any new payload objects
* from what was provided
*/
@Override
public void process(IBaseDataObject payload) throws ResourceException {
if (heavyDutyMethodImplemented) {
List<IBaseDataObject> children = processHeavyDuty(payload);
if (children != null && !children.isEmpty()) {
logger.error("Sprouting is no longer supported, lost {} children", children.size());
}
} else {
throw new IllegalStateException("Neither process nor processHeavyDuty appears to be implemented");
}
}
/**
* Process a payload and return a list of new items decomosed from it (or an empty list) What happens here and what is
* ultimately expected depends on the workflow stage of the place registration, and the type of job it is expected to
* do.
*
* @param payload the BaseDataObject to process
* @return list of BaseDataObject that represent the children (if any)
*/
@Override
public List<IBaseDataObject> processHeavyDuty(IBaseDataObject payload) throws ResourceException {
if (processMethodImplemented) {
process(payload);
return Collections.emptyList();
} else {
throw new IllegalStateException("Neither process nor processHeavyDuty appears to be implemented");
}
}
/**
* This method must be called during setup of the place to ensure that one of the two implementations is provided by the
* declaring class.
*/
protected void verifyProcessImplementationProvided() {
Class<?> c = this.getClass();
while (!c.isAssignableFrom(ServiceProviderPlace.class)) {
for (Method m : c.getDeclaredMethods()) {
String mname = m.getName();
String rname = m.getReturnType().getName();
Class<?>[] params = m.getParameterTypes();
if (params.length == 1 && params[0].isAssignableFrom(IBaseDataObject.class)) {
if (mname.equals("process") && rname.equals("void")) {
processMethodImplemented = true;
} else if (mname.equals("processHeavyDuty") && rname.equals(List.class.getName())) {
heavyDutyMethodImplemented = true;
}
}
}
if (heavyDutyMethodImplemented || processMethodImplemented) {
logger.debug("Found enough process implementation at level {}", c.getName());
break;
} else {
c = c.getSuperclass();
}
}
if (!processMethodImplemented && !heavyDutyMethodImplemented && !(this instanceof AgentsNotSupportedPlace)) {
logger.error("It appears that neither process nor processHeavyDuty is implemented. "
+ "If that is incorrect you can directly set one of the corresponding "
+ "boolean flags or override verifyProcessImplementationProvided or "
+ "implement AgentsNotSupported to turn this message off");
}
}
/**
* Convenience method a lot of places use. Removes all items from the current form stack that this place has proxies for
*
* @param d a data object whose current form will be expunged of my proxies
* @return count of how many items removed
*/
protected int nukeMyProxies(IBaseDataObject d) {
List<String> nukem = new ArrayList<>();
int sz = d.currentFormSize();
Set<String> serviceProxies = getProxies();
if (serviceProxies.contains("*")) {
d.replaceCurrentForm(null); // clear it out
return sz;
}
// listem
for (int i = 0; i < sz; i++) {
String form = d.currentFormAt(i);
if (serviceProxies.contains(form)) {
nukem.add(form);
} else {
// cardem
WildcardEntry wc = new WildcardEntry(form);
if (!Collections.disjoint(wc.asSet(), serviceProxies)) {
nukem.add(form);
}
}
}
// nukem
for (String f : nukem) {
int pos = d.searchCurrentForm(f);
if (pos != -1) {
d.deleteCurrentFormAt(pos);
}
}
return nukem.size();
}
/**
* Return a set of the service proxies
*
* @return set of string values
*/
@Override
public Set<String> getProxies() {
Set<String> s = new TreeSet<>();
for (String k : keys) {
s.add(KeyManipulator.getDataType(k));
}
s.remove(UNUSED_PROXY);
return s;
}
/**
* Get the keys that this place instance is registered with
*/
@Override
public Set<String> getKeys() {
Set<String> s = new TreeSet<>();
for (String k : keys) {
if (!k.startsWith(UNUSED_PROXY)) {
s.add(k);
}
}
return s;
}
/**
* Return the first service proxy on the list
*
* @return SERVICE_PROXY value from first key on list
*/
@Override
public String getPrimaryProxy() {
String s = KeyManipulator.getDataType(keys.get(0));
if (s.equals(UNUSED_PROXY)) {
s = "";
}
return s;
}
/**
* Key for string form
*/
@Override
public String toString() {
return keys.get(0) + "[" + keys.size() + "]";
}
/**
* Fulfill IServiceProviderPlace
*/
@Override
public String getKey() {
return KeyManipulator.removeExpense(keys.get(0));
}
/**
* Add a service proxy to a running place. Duplicates are ignored.
*
* @param serviceProxy the new proxy string to add
*/
@Override
public void addServiceProxy(String serviceProxy) {
// Add new one to the top key in the list
DirectoryEntry de = new DirectoryEntry(keys.get(0));
boolean keyAdded = false;
if (!de.getDataType().equals(serviceProxy)) {
// Clear out the placeholder
if (de.getDataType().equals(UNUSED_PROXY)) {
keys.remove(0);
}
de.setDataType(serviceProxy);
if (!keys.contains(de.getFullKey())) {
keys.add(de.getFullKey());
// Register the new proxy in the directory
logger.debug("Registering new key {}", de.getKey());
register(de.getFullKey());
keyAdded = true;
}
}
if (!keyAdded) {
logger.debug("Duplicate service proxy {} ignored", serviceProxy);
}
}
/**
* Add another key to the place
*
* @param key the key to add
*/
@Override
public void addKey(String key) {
if (KeyManipulator.isValid(key)) {
DirectoryEntry de = new DirectoryEntry(keys.get(0));
if (de.getDataType().equals(UNUSED_PROXY)) {
keys.remove(0);
}
logger.debug("Adding and registering new key {}", key);
keys.add(key);
register(key);
} else {
logger.warn("Invalid key cannot be added: {}", key);
}
}
/**
* Register a single service proxy key
*
* @param key the new key to register
*/
protected void register(String key) {
logger.debug("Registering key {}", key);
// Cannot register if we have no directory
// If we are the directory, its no problem though
if (dirPlace == null) {
if (!(this instanceof IDirectoryPlace)) {
logger.debug("Directory is null: cannot register anything. Illegal configuration.");
}
return;
}
// Register place and all proxies by building up a list
// of our interests to send to the directory
List<String> keylist = new ArrayList<>();
keylist.add(key);
registerWithDirectory(keylist);
}
/**
* Register our interest in all of our serviceProxies Sends only one message to the directory to cover all service
* proxies (a scalability issue for large systems)
*/
protected void register() {
logger.debug("Registering: {}", this);
// Cannot register if we have no directory
// If we are the directory, its no problem though
if (dirPlace == null) {
if (!(this instanceof IDirectoryPlace)) {
logger.debug("Directory is null: cannot register anything. Illegal configuration.");
}
return;
}
// Register place and all proxies by building up a list
// of our current interests to send to the directory
registerWithDirectory(new ArrayList<>(keys));
}
/**
* Register keys with the directory
*
* @param keylist the keys to register
*/
protected void registerWithDirectory(List<String> keylist) {
try {
if (localDirPlace == null) {
// This should never happen, we require a local
// directory on every JVM...
logger.error("Key registration requires a DirectoryPlace in every Emissary Node");
} else {
logger.debug("Registering my {} keys {}", keylist.size(), keylist);
localDirPlace.addPlaces(keylist);
}
} catch (Exception e) {
logger.warn("Register ERROR for keys {}", keylist, e);
}
}
/**
* Deregister keys from the directory
*
* @param keys the keys to register
*/
protected void deregisterFromDirectory(List<String> keys) {
try {
if (localDirPlace == null && dirPlace != null) {
// This should never happen, we require a local
// directory on every JVM...
logger.debug("Deregistering my {} proxies {} from remote dir {}", keys.size(), keys, dirPlace);
DirectoryAdapter da = new DirectoryAdapter();
da.outboundRemovePlaces(dirPlace, keys, false);
} else if (localDirPlace != null) {
logger.debug("Deregistering my {} proxies {}", keys.size(), keys);
localDirPlace.removePlaces(keys);
}
} catch (Exception e) {
logger.warn("Deregister ERROR keys={}", keys, e);
}
}
/**
* Remove a service proxy from the running place. Proxy strings not found registered will be ignored Will remove all
* keys that match the supplied proxy
*
* @param serviceProxy the proxy string to remove
*/
@Override
public void removeServiceProxy(String serviceProxy) {
// List of keys to deregister
List<String> keylist = new ArrayList<>();
// nb. no enhanced for loop due to remove
for (Iterator<String> i = keys.iterator(); i.hasNext();) {
String k = i.next();
String kproxy = KeyManipulator.getDataType(k);
if (kproxy.equals(serviceProxy)) {
keylist.add(KeyManipulator.removeExpense(k));
i.remove();
}
}
if (!keylist.isEmpty()) {
deregisterFromDirectory(keylist);
} else {
logger.debug("Unknown service proxy {} ignored", serviceProxy);
}
// Make sure we leave something on the keys list
if (keys.isEmpty() && !keylist.isEmpty()) {
DirectoryEntry de = new DirectoryEntry(keylist.get(0));
de.setCost(serviceCost);
de.setQuality(serviceQuality);
de.setDataType(UNUSED_PROXY);
keys.add(de.getFullKey());
}
}
/**
* Remove and deregister a key
*
* @param key the full key (with expense) to deregister
*/
@Override
public void removeKey(String key) {
String keyWithOutExpense = KeyManipulator.removeExpense(key);
if (keys.remove(key)) {
List<String> keylist = new ArrayList<>();
keylist.add(keyWithOutExpense);
deregisterFromDirectory(keylist);
if (keys.isEmpty()) {
DirectoryEntry de = new DirectoryEntry(key);
de.setDataType(UNUSED_PROXY);
keys.add(de.getFullKey());
}
} else {
logger.debug("Key specified for removal not found: {}", key);
}
}
/**
* Stop all threads if any. Can be overridden by, deregister from directory, and remove from the namespace. derived
* classes if needed
*/
@Override
public void shutDown() {
logger.debug("Called shutDown()");
// Remove from directory a list of keys without expense
deregisterFromDirectory(keys.stream().map(KeyManipulator::removeExpense).collect(Collectors.toList()));