View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.service.flatfile.impl;
17  
18  import java.util.Locale;
19  
20  import org.aludratest.config.Preferences;
21  import org.aludratest.content.flat.FlatContent;
22  import org.aludratest.exception.TechnicalException;
23  import org.aludratest.service.AbstractConfigurableAludraService;
24  import org.aludratest.service.Implementation;
25  import org.aludratest.service.file.FileService;
26  import org.aludratest.service.flatfile.FlatFileCondition;
27  import org.aludratest.service.flatfile.FlatFileInteraction;
28  import org.aludratest.service.flatfile.FlatFileService;
29  import org.aludratest.service.flatfile.FlatFileVerification;
30  
31  /**
32   * Default {@link FlatFileService} implementation.
33   * @author Volker Bergmann
34   */
35  @Implementation({ FlatFileService.class })
36  public class DefaultFlatFileService extends AbstractConfigurableAludraService implements FlatFileService {
37  
38  
39      /** Implementor of all action interfaces of the FlatFileService. */
40      private DefaultFlatFileAction action;
41  
42      private FileService fileService;
43  
44      private FlatFileConfig configuration;
45  
46      /** Public default constructor. */
47      public DefaultFlatFileService() {
48      }
49  
50      @Override
51      public FileService getFileService() {
52          return fileService;
53      }
54  
55      /** Provides an object that implements the {@link FlatFileInteraction} interface. */
56      @Override
57      public FlatFileInteraction perform() {
58          return action;
59      }
60  
61      /** Provides an object that implements the {@link FlatFileVerification} interface. */
62      @Override
63      public FlatFileVerification verify() {
64          return action;
65      }
66  
67      /** Provides an object that implements the {@link FlatFileCondition} interface. */
68      @Override
69      public FlatFileCondition check() {
70          return action;
71      }
72  
73      @Override
74      public void configure(Preferences preferences) {
75          configuration = new FlatFileConfig(preferences);
76      }
77  
78      @Override
79      public String getPropertiesBaseName() {
80          return "flatfile";
81      }
82  
83      /** Callback method that is called by the AludraTest framework
84       * and initializes the service. */
85      @Override
86      public void initService() {
87          try {
88              Locale locale = configuration.getLocale();
89              fileService = aludraServiceContext.getNonLoggingService(FileService.class);
90              FlatContent contentHandler = aludraServiceContext.newComponentInstance(FlatContent.class);
91              contentHandler.setLocale(locale);
92              this.action = new DefaultFlatFileAction(contentHandler, fileService);
93          } catch (Exception e) {
94              throw new TechnicalException("Error initializing " + this, e);
95          }
96      }
97  
98      /** Closes the service. */
99      @Override
100     public void close() {
101         // nothing to do
102     }
103 
104     @Override
105     public String getDescription() {
106         return toString();
107     }
108 
109 }