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.file.impl;
17  
18  import org.aludratest.config.ConfigProperties;
19  import org.aludratest.config.ConfigProperty;
20  import org.aludratest.config.Preferences;
21  import org.aludratest.exception.AutomationException;
22  import org.aludratest.service.AbstractConfigurableAludraService;
23  import org.aludratest.service.Implementation;
24  import org.aludratest.service.file.FileCondition;
25  import org.aludratest.service.file.FileInteraction;
26  import org.aludratest.service.file.FileService;
27  import org.aludratest.service.file.FileVerification;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Implements the {@link FileService} interface.
34   * @author Volker Bergmann
35   */
36  @Implementation({ FileService.class })
37  @ConfigProperties({
38          @ConfigProperty(name = "wait.max.retries", type = int.class, description = "The maximum number of retries when polling files.", defaultValue = ""
39                  + FileServiceConfiguration.DEFAULT_WAIT_MAX_RETRIES),
40          @ConfigProperty(name = "wait.timeout", type = int.class, description = "The maximum time to wait when polling, in milliseconds. The retries are distributed over this time", defaultValue = ""
41                  + FileServiceConfiguration.DEFAULT_WAIT_TIMEOUT) })
42  public class FileServiceImpl extends AbstractConfigurableAludraService implements FileService {
43  
44      /** The logger of the class. */
45      private static final Logger LOGGER = LoggerFactory.getLogger(FileServiceImpl.class);
46  
47      private FileServiceConfiguration configuration;
48  
49      /** The implementor of all action interfaces. */
50      private FileActionImpl action;
51  
52      /** Default constructor. */
53      public FileServiceImpl() {
54          // nothing to do here
55      }
56  
57      // Configurable interface implementation ----------------------------------
58  
59      @Override
60      public String getPropertiesBaseName() {
61          return "fileService";
62      }
63  
64      @Override
65      public void configure(Preferences preferences) throws AutomationException {
66          try {
67              configuration = new FileServiceConfiguration(preferences);
68          }
69          catch (FileSystemException e) {
70              LOGGER.error("Error configuring FileService", e);
71              throw new AutomationException("Error initializing " + this, e);
72          }
73      }
74  
75      // AludraService interface implementation ----------------------------------
76      @Override
77      public void initService() {
78          this.action = new FileActionImpl(configuration);
79      }
80  
81      /** Closes the configuration (and with it, Commons VFS' StandardFileSystemManager). */
82      @Override
83      public void close() {
84          this.configuration.close();
85      }
86  
87      /** Provides the service's interaction operations. */
88      @Override
89      public FileInteraction perform() {
90          return this.action;
91      }
92  
93      /** Provides the service's verification operations. */
94      @Override
95      public FileVerification verify() {
96          return this.action;
97      }
98  
99      /** Provides the service's checking operations. */
100     @Override
101     public FileCondition check() {
102         return this.action;
103     }
104 
105     @Override
106     public String getDescription() {
107         String description = "Accessing " + configuration.getBaseUrl();
108         if (configuration.getUser() != null) {
109             description += " as user " + configuration.getUser();
110         }
111         return description;
112     }
113 
114 }