View Javadoc
1   /*
2    * Copyright 2020 the original author or authors.
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  
17  package org.bremersee.data.minio;
18  
19  import io.minio.MinioClient;
20  import org.springframework.util.ErrorHandler;
21  
22  /**
23   * The minio template.
24   *
25   * @author Christian Bremer
26   */
27  public class MinioTemplate implements MinioOperations, Cloneable {
28  
29    private final MinioClient client;
30  
31    private ErrorHandler errorHandler = new DefaultMinioErrorHandler();
32  
33    /**
34     * Instantiates a new minio template.
35     *
36     * @param client the client
37     */
38    public MinioTemplate(MinioClient client) {
39      this.client = client;
40    }
41  
42    /**
43     * Sets error handler.
44     *
45     * @param errorHandler the error handler
46     */
47    public void setErrorHandler(ErrorHandler errorHandler) {
48      if (errorHandler != null) {
49        this.errorHandler = errorHandler;
50      }
51    }
52  
53    @SuppressWarnings("MethodDoesntCallSuperMethod")
54    @Override
55    public MinioTemplate clone() {
56      return clone(null);
57    }
58  
59    /**
60     * Clone minio template.
61     *
62     * @param errorHandler the error handler
63     * @return the minio template
64     */
65    public MinioTemplate clone(ErrorHandler errorHandler) {
66      MinioTemplatelate.html#MinioTemplate">MinioTemplate clone = new MinioTemplate(client);
67      clone.setErrorHandler(errorHandler);
68      return clone;
69    }
70  
71    @Override
72    public <T> T execute(MinioClientCallback<T> callback) {
73      try {
74        return callback.doWithMinioClient(client);
75  
76      } catch (Exception e) {
77        errorHandler.handleError(e);
78        return null;
79      }
80    }
81  
82  }