View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.recyclable.impl;
33  
34  import org.spf4j.recyclable.RecyclingSupplier;
35  import java.io.IOException;
36  import java.nio.channels.ClosedChannelException;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * @author zoly
42   */
43  public final class ExpensiveTestObjectFactory implements RecyclingSupplier.Factory<ExpensiveTestObject> {
44  
45      private static final Logger LOG = LoggerFactory.getLogger(ExpensiveTestObjectFactory.class);
46  
47      private final long maxIdleMillis;
48      private final int nrUsesToFailAfter;
49      private final long  minOperationMillis;
50      private final long maxOperationMillis;
51  
52      public ExpensiveTestObjectFactory(final long maxIdleMillis, final int nrUsesToFailAfter,
53              final long minOperationMillis, final long maxOperationMillis) {
54          this.maxIdleMillis = maxIdleMillis;
55          this.nrUsesToFailAfter = nrUsesToFailAfter;
56          this.minOperationMillis = minOperationMillis;
57          this.maxOperationMillis = maxOperationMillis;
58      }
59  
60      public ExpensiveTestObjectFactory() {
61          this(100, 10, 1, 20);
62      }
63  
64  
65  
66      @Override
67      public ExpensiveTestObject create() {
68          return new ExpensiveTestObject(maxIdleMillis, nrUsesToFailAfter, minOperationMillis, maxOperationMillis);
69      }
70  
71      @Override
72      public void dispose(final ExpensiveTestObject object) {
73          try {
74              object.close();
75          } catch (ClosedChannelException ex) {
76            // connection is already closed.
77          } catch (IOException ex) {
78            LOG.warn("Cannot dispose object {}", this, ex);
79          }
80      }
81  
82      @Override
83      public boolean validate(final ExpensiveTestObject object, final Exception e) throws IOException {
84         if (e instanceof IOException) {
85             return false;
86         } else {
87              object.testObject();
88              return true;
89         }
90      }
91  
92    @Override
93    public String toString() {
94      return "ExpensiveTestObjectFactory{" + "maxIdleMillis=" + maxIdleMillis
95              + ", nrUsesToFailAfter=" + nrUsesToFailAfter + ", minOperationMillis="
96              + minOperationMillis + ", maxOperationMillis=" + maxOperationMillis + '}';
97    }
98  
99  
100 
101 }