View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2015-2016 ForgeRock AS.
15   */
16  package org.forgerock.selfservice.json;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.forgerock.json.JsonValue;
22  import org.forgerock.selfservice.core.AnonymousProcessService;
23  import org.forgerock.selfservice.core.ProcessStore;
24  import org.forgerock.selfservice.core.ProgressStageProvider;
25  import org.forgerock.selfservice.core.config.ProcessInstanceConfig;
26  import org.forgerock.selfservice.core.config.StageConfig;
27  import org.forgerock.selfservice.core.snapshot.SnapshotTokenHandlerFactory;
28  import org.forgerock.util.Reject;
29  
30  /**
31   * Builder for {@link AnonymousProcessService} from JSON config and AnonymousProcessService requirements.
32   *
33   * @since 20.0.0
34   */
35  public final class JsonAnonymousProcessServiceBuilder {
36  
37      private ClassLoader classLoader = getClass().getClassLoader(); // assume this ClassLoader if not supplied
38      private Map<String, Class<? extends StageConfig>> stageConfigMappings = new HashMap<>();
39      private JsonValue jsonConfig;
40      private ProgressStageProvider progressStageProvider;
41      private SnapshotTokenHandlerFactory tokenHandlerFactory;
42      private ProcessStore processStore;
43  
44      private JsonAnonymousProcessServiceBuilder() {
45          // prevent direct instantiation
46      }
47  
48      /**
49       * Construct a new JsonAnonymousProcessServiceBuilder.
50       *
51       * @return the JsonAnonymousProcesssServiceBuilder.
52       */
53      public static JsonAnonymousProcessServiceBuilder newBuilder() {
54          return new JsonAnonymousProcessServiceBuilder();
55      }
56  
57      /**
58       * Set the ClassLoader.
59       *
60       * @param classLoader the ClassLoader
61       * @return this builder instance
62       */
63      public JsonAnonymousProcessServiceBuilder withClassLoader(ClassLoader classLoader) {
64          this.classLoader = classLoader;
65          return this;
66      }
67  
68      /**
69       * Provide additional named type-mapping, if desired.
70       *
71       * @param name the {@code name} attribute value to associate with the new stage config type
72       * @param type the {@link StageConfig} type to associate
73       * @return this builder instance
74       */
75      public JsonAnonymousProcessServiceBuilder withStageConfigMapping(String name, Class<? extends StageConfig> type) {
76          this.stageConfigMappings.put(name, type);
77          return this;
78      }
79  
80      /**
81       * Set the JSON config from which to build the {@link ProcessInstanceConfig}.
82       *
83       * @param jsonConfig the JSON config for a {@link ProcessInstanceConfig}.
84       * @return this builder instance
85       */
86      public JsonAnonymousProcessServiceBuilder withJsonConfig(JsonValue jsonConfig) {
87          this.jsonConfig = jsonConfig;
88          return this;
89      }
90  
91      /**
92       * Sets the {@link ProgressStageProvider}.
93       *
94       * @param progressStageProvider the {@link ProgressStageProvider}.
95       * @return this builder instance
96       */
97      public JsonAnonymousProcessServiceBuilder withProgressStageProvider(ProgressStageProvider progressStageProvider) {
98          this.progressStageProvider = progressStageProvider;
99          return this;
100     }
101 
102     /**
103      * Sets the {@link SnapshotTokenHandlerFactory}.
104      *
105      * @param tokenHandlerFactory the {@link SnapshotTokenHandlerFactory}.
106      * @return this builder instance
107      */
108     public JsonAnonymousProcessServiceBuilder withTokenHandlerFactory(SnapshotTokenHandlerFactory tokenHandlerFactory) {
109         this.tokenHandlerFactory = tokenHandlerFactory;
110         return this;
111     }
112 
113     /**
114      * Sets the {@link ProcessStore}.
115      *
116      * @param processStore the {@link ProcessStore}.
117      * @return this builder instance
118      */
119     public JsonAnonymousProcessServiceBuilder withProcessStore(ProcessStore processStore) {
120         this.processStore = processStore;
121         return this;
122     }
123 
124     /**
125      * Build an {@link AnonymousProcessService} from the JSON config and the other elements.
126      *
127      * @return the {@link AnonymousProcessService}
128      */
129     public AnonymousProcessService build() {
130         Reject.ifNull(classLoader, jsonConfig, progressStageProvider, tokenHandlerFactory, processStore);
131         ProcessInstanceConfig config = new JsonConfig(classLoader, stageConfigMappings)
132                 .buildProcessInstanceConfig(jsonConfig);
133         Reject.ifNull(config.getStageConfigs(), config.getSnapshotTokenConfig(), config.getStorageType());
134         Reject.ifTrue(config.getStageConfigs().isEmpty());
135         return new AnonymousProcessService(config, progressStageProvider, tokenHandlerFactory, processStore,
136                 classLoader);
137     }
138 }