Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2014-2015 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  * either express or implied. See the License for the specific language
  * governing permissions and limitations under the License.
  */
 package org.dbflute.infra.manage.refresh;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
The request of (Eclipse's) refresh resource.
You can refresh automatically by this.
 DfRefreshResourceRequest request
     = new DfRefreshResourceRequest(projectNameList, requestUrl);
 request.refreshResources();
 

Author(s):
jflute
 
 public class DfRefreshResourceRequest {
 
     // ===================================================================================
     //                                                                          Definition
     //                                                                          ==========
     
The mark of auto-detect, e.g. searching Eclipse .project file. (NotNull)
 
     public static final String AUTO_DETECT_MARK = "$$AutoDetect$$"// basically for engine
 
    
The key of result map for response body. (NotNull)
 
     public static final String KEY_BODY = "body";

    
The key of result map for header fields. (NotNull)
 
     public static final String KEY_HEADER_FIELDS = "headerFields";
 
     // ===================================================================================
     //                                                                           Attribute
     //                                                                           =========
     protected final List<String_projectNameList;
     protected final String _requestUrl;
 
     // ===================================================================================
     //                                                                         Constructor
     //                                                                         ===========
     

Parameters:
projectNameList The list of project name for refresh. (NotNull)
requestUrl The request URL for refresh to synchronizer. (NotNull)
 
     public DfRefreshResourceRequest(List<StringprojectNameListString requestUrl) {
         if (projectNameList == null || projectNameList.isEmpty()) {
             String msg = "The argument 'projectNameList' should not be null or empty: " + projectNameList;
             throw new IllegalArgumentException(msg);
         }
         if (Srl.is_Null_or_TrimmedEmpty(requestUrl)) {
             String msg = "The argument 'requestUrl' should not be null or empty: " + requestUrl;
             throw new IllegalArgumentException(msg);
         }
          = projectNameList;
          = requestUrl;
     }
 
     // ===================================================================================
     //                                                                             Refresh
     //                                                                             =======
     
Refresh resources per one project. (request to synchronizer)

Returns:
The map of result. map:{projectName = map:{body = ...}} (NotNull)
Throws:
java.io.IOException When the refresh failed.
 
     public Map<StringMap<StringObject>> refreshResources() throws IOException {
         final Map<StringMap<StringObject>> resultMap = newResultMap();
         for (String projectName : ) {
             resultMap.put(projectNamedoRefreshResources(projectName));
         }
         return resultMap;
     }
 
     protected Map<StringMap<StringObject>> newResultMap() {
         return new LinkedHashMap<StringMap<StringObject>>();
     }
 
     protected Map<StringObjectdoRefreshResources(String projectNamethrows IOException {
        final String pathAndQuery = buildRefreshPathAndQuery(projectName);
        final URL url = createRefreshRequestURL(pathAndQuery);
        if (url == null) {
            return null;
        }
        InputStream ins = null;
        try {
            final URLConnection conn = openConnect(url);
            ins = conn.getInputStream();
            final Map<StringObjectelementMap = newElementMap();
            setupBasicResult(connelementMapins);
            handleConnectedConnection(connelementMap);
            return elementMap;
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException ignored) {}
            }
        }
    }
    protected LinkedHashMap<StringObjectnewElementMap() {
        return new LinkedHashMap<StringObject>();
    }
    protected String buildRefreshPathAndQuery(String projectName) {
        final StringBuilder sb = new StringBuilder();
        sb.append("refresh?").append(projectName).append("=INFINITE");
        return sb.toString();
    }
    protected void setupBasicResult(URLConnection connMap<StringObjectelementMapInputStream insthrows IOException {
        final String body = extractResponseBody(ins);
        elementMap.put(body);
        elementMap.put(conn.getHeaderFields());
    }
    protected String extractResponseBody(InputStream insthrows IOException {
        final byte[] body = new byte[1024];
        ins.read(body);
        return new String(body"UTF-8");
    }
    protected void handleConnectedConnection(URLConnection connMap<StringObjectelementMapthrows IOException {
        // do nothing as default: for overriding
    }
    // ===================================================================================
    //                                                                   Connection Helper
    //                                                                   =================
    protected URLConnection openConnect(URL urlthrows IOException {
        final URLConnection conn = url.openConnection();
        conn.connect();
        return conn;
    }
    protected URL createRefreshRequestURL(String paththrows MalformedURLException {
        String requestUrl = ;
        if (!requestUrl.endsWith("/")) {
            requestUrl = requestUrl + "/";
        }
        return new URL(requestUrl + path);
    }
    protected int getRefreshRequestReadTimeout() {
        return 3 * 1000;
    }
New to GrepCode? Check out our FAQ X