Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 package org.apache.hive.service;
0019 
0020 import java.io.IOException;
0021 
0022 import org.slf4j.Logger;
0023 
0024 public class ServiceUtils {
0025 
0026   /*
0027    * Get the index separating the user name from domain name (the user's name up
0028    * to the first '/' or '@').
0029    *
0030    * @param userName full user name.
0031    * @return index of domain match or -1 if not found
0032    */
0033   public static int indexOfDomainMatch(String userName) {
0034     if (userName == null) {
0035       return -1;
0036     }
0037 
0038     int idx = userName.indexOf('/');
0039     int idx2 = userName.indexOf('@');
0040     int endIdx = Math.min(idx, idx2); // Use the earlier match.
0041     // Unless at least one of '/' or '@' was not found, in
0042     // which case, user the latter match.
0043     if (endIdx == -1) {
0044       endIdx = Math.max(idx, idx2);
0045     }
0046     return endIdx;
0047   }
0048 
0049   /**
0050    * Close the Closeable objects and <b>ignore</b> any {@link IOException} or
0051    * null pointers. Must only be used for cleanup in exception handlers.
0052    *
0053    * @param log the log to record problems to at debug level. Can be null.
0054    * @param closeables the objects to close
0055    */
0056   public static void cleanup(Logger log, java.io.Closeable... closeables) {
0057     for (java.io.Closeable c : closeables) {
0058       if (c != null) {
0059         try {
0060           c.close();
0061         } catch(IOException e) {
0062           if (log != null && log.isDebugEnabled()) {
0063             log.debug("Exception in closing " + c, e);
0064           }
0065         }
0066       }
0067     }
0068   }
0069 
0070 }