ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [07/11] ignite git commit: IGNITE-3202: Better basic mapper.
Date Mon, 30 May 2016 16:05:17 GMT
IGNITE-3202: Better basic mapper.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/fca684d4
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/fca684d4
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/fca684d4

Branch: refs/heads/ignite-3202
Commit: fca684d4deb5a916fe4f36ab0585f8cebe7ba088
Parents: 86e26cd
Author: vozerov-gridgain <vozerov@gridgain.com>
Authored: Mon May 30 11:38:17 2016 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Mon May 30 11:38:17 2016 +0300

----------------------------------------------------------------------
 .../ignite/hadoop/util/BasicUserNameMapper.java | 46 ++++++++-----
 .../hadoop/util/ConstantUserNameMapper.java     | 64 ------------------
 .../util/BasicUserNameMapperSelfTest.java       | 70 +++++++++-----------
 3 files changed, 61 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/fca684d4/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/BasicUserNameMapper.java
----------------------------------------------------------------------
diff --git a/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/BasicUserNameMapper.java
b/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/BasicUserNameMapper.java
index e83c2be..1ed7701 100644
--- a/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/BasicUserNameMapper.java
+++ b/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/BasicUserNameMapper.java
@@ -25,15 +25,19 @@ import org.jetbrains.annotations.Nullable;
 import java.util.Map;
 
 /**
- * Name mapper which maps one user name to another based on predefined dictionary.
+ * Name mapper which maps one user name to another based on predefined dictionary. If name
is not found in the
+ * dictionary, or dictionary is not defined, either passed user name or some default value
could be returned.
  */
-public class BasicUserNameMapper implements UserNameMapper, LifecycleAware {
+public class BasicUserNameMapper implements UserNameMapper {
     /** */
     private static final long serialVersionUID = 0L;
 
     /** Mappings. */
     private Map<String, String> mappings;
 
+    /** Whether to use default user name. */
+    private boolean useDfltUsrName;;
+
     /** Default user name. */
     private String dfltUsrName;
 
@@ -43,18 +47,7 @@ public class BasicUserNameMapper implements UserNameMapper, LifecycleAware
{
 
         String res = mappings.get(name);
 
-        return res != null ? res : dfltUsrName;
-    }
-
-    /** {@inheritDoc} */
-    @Override public void start() throws IgniteException {
-        if (mappings == null)
-            throw new IgniteException("Mappings cannot be null.");
-    }
-
-    /** {@inheritDoc} */
-    @Override public void stop() throws IgniteException {
-        // No-op.
+        return res != null ? res : useDfltUsrName ? dfltUsrName : name;
     }
 
     /**
@@ -62,7 +55,7 @@ public class BasicUserNameMapper implements UserNameMapper, LifecycleAware
{
      *
      * @return Mappings.
      */
-    public Map<String, String> getMappings() {
+    @Nullable public Map<String, String> getMappings() {
         return mappings;
     }
 
@@ -71,14 +64,33 @@ public class BasicUserNameMapper implements UserNameMapper, LifecycleAware
{
      *
      * @param mappings Mappings.
      */
-    public void setMappings(Map<String, String> mappings) {
+    public void setMappings(@Nullable Map<String, String> mappings) {
         this.mappings = mappings;
     }
 
     /**
+     * Get whether to use default user name when there is no mapping for current user name.
+     *
+     * @return Whether to use default user name.
+     */
+    public boolean isUseDefaultUserName() {
+        return useDfltUsrName;
+    }
+
+    /**
+     * Set whether to use default user name when there is no mapping for current user name.
+     *
+     * @param useDfltUsrName Whether to use default user name.
+     */
+    public void setUseDefaultUserName(boolean useDfltUsrName) {
+        this.useDfltUsrName = useDfltUsrName;
+    }
+
+    /**
      * Get default user name (optional).
      * <p>
-     * This user name will be used if provided mappings doesn't contain mapping for the given
user name.
+     * This user name will be used if provided mappings doesn't contain mapping for the given
user name and
+     * {#isUseDefaultUserName} is set to {@code true}.
      * <p>
      * Defaults to {@code null}.
      *

http://git-wip-us.apache.org/repos/asf/ignite/blob/fca684d4/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/ConstantUserNameMapper.java
----------------------------------------------------------------------
diff --git a/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/ConstantUserNameMapper.java
b/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/ConstantUserNameMapper.java
deleted file mode 100644
index 6576596..0000000
--- a/modules/hadoop/src/main/java/org/apache/ignite/hadoop/util/ConstantUserNameMapper.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.apache.ignite.hadoop.util;
-
-import org.apache.ignite.internal.util.typedef.internal.S;
-import org.jetbrains.annotations.Nullable;
-
-/**
- * Constant user name mapper. Performs all file system requests with the same user name.
- * <p>
- * If {@code null} is set as a name, name of the current process owner will be used.
- */
-public class ConstantUserNameMapper implements UserNameMapper {
-    /** */
-    private static final long serialVersionUID = 0L;
-
-    /** User name. */
-    private String usrName;
-
-    /** {@inheritDoc} */
-    @Nullable @Override public String map(String name) {
-        return usrName;
-    }
-
-    /**
-     * Get user name to be user for all file system requests.
-     * <p>
-     * When set to {@code null}, name of the current process owner will be used.
-     *
-     * @return User name.
-     */
-    public String getUserName() {
-        return usrName;
-    }
-
-    /**
-     * Set user name. See {@link #getUserName()} for more information.
-     *
-     * @param usrName User name.
-     */
-    public void setUserName(@Nullable String usrName) {
-        this.usrName = usrName;
-    }
-
-    /** {@inheritDoc} */
-    @Override public String toString() {
-        return S.toString(ConstantUserNameMapper.class, this);
-    }
-}

http://git-wip-us.apache.org/repos/asf/ignite/blob/fca684d4/modules/hadoop/src/test/java/org/apache/ignite/hadoop/util/BasicUserNameMapperSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/hadoop/src/test/java/org/apache/ignite/hadoop/util/BasicUserNameMapperSelfTest.java
b/modules/hadoop/src/test/java/org/apache/ignite/hadoop/util/BasicUserNameMapperSelfTest.java
index 7a3be32..6e257b0 100644
--- a/modules/hadoop/src/test/java/org/apache/ignite/hadoop/util/BasicUserNameMapperSelfTest.java
+++ b/modules/hadoop/src/test/java/org/apache/ignite/hadoop/util/BasicUserNameMapperSelfTest.java
@@ -18,43 +18,16 @@
 package org.apache.ignite.hadoop.util;
 
 import org.apache.avro.reflect.Nullable;
-import org.apache.ignite.IgniteException;
-import org.apache.ignite.testframework.GridTestUtils;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 
 import java.util.HashMap;
 import java.util.Map;
-import java.util.concurrent.Callable;
 
 /**
  * Test for basic user name mapper.
  */
 public class BasicUserNameMapperSelfTest extends GridCommonAbstractTest {
     /**
-     * Test mapper with null mappings.
-     *
-     * @throws Exception If failed.
-     */
-    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
-    public void testNullMappings() throws Exception {
-        GridTestUtils.assertThrows(null, new Callable<Void>() {
-            @Override public Void call() throws Exception {
-                create(null, null);
-
-                return null;
-            }
-        }, IgniteException.class, null);
-
-        GridTestUtils.assertThrows(null, new Callable<Void>() {
-            @Override public Void call() throws Exception {
-                create(null, "A");
-
-                return null;
-            }
-        }, IgniteException.class, null);
-    }
-
-    /**
      * Test empty mappings.
      *
      * @throws Exception If failed.
@@ -62,17 +35,27 @@ public class BasicUserNameMapperSelfTest extends GridCommonAbstractTest
{
     public void testEmptyMappings() throws Exception {
         Map<String, String> map = new HashMap<>();
 
-        BasicUserNameMapper mapper = create(map, null);
+        BasicUserNameMapper mapper = create(map, false, null);
+
+        assertNull(mapper.map(null));
+        assertEquals("1", mapper.map("1"));
+        assertEquals("2", mapper.map("2"));
+
+        mapper = create(map, true, null);
 
         assertNull(mapper.map(null));
-        assertNull(mapper.map(""));
         assertNull(mapper.map("1"));
         assertNull(mapper.map("2"));
 
-        mapper = create(map, "A");
+        mapper = create(map, false, "A");
+
+        assertNull(mapper.map(null));
+        assertEquals("1", mapper.map("1"));
+        assertEquals("2", mapper.map("2"));
+
+        mapper = create(map, true, "A");
 
         assertEquals("A", mapper.map(null));
-        assertEquals("A", mapper.map(""));
         assertEquals("A", mapper.map("1"));
         assertEquals("A", mapper.map("2"));
     }
@@ -87,17 +70,27 @@ public class BasicUserNameMapperSelfTest extends GridCommonAbstractTest
{
 
         map.put("1", "101");
 
-        BasicUserNameMapper mapper = create(map, null);
+        BasicUserNameMapper mapper = create(map, false, null);
+
+        assertNull(mapper.map(null));
+        assertEquals("101", mapper.map("1"));
+        assertEquals("2", mapper.map("2"));
+
+        mapper = create(map, true, null);
 
         assertNull(mapper.map(null));
-        assertNull(mapper.map(""));
         assertEquals("101", mapper.map("1"));
         assertNull(mapper.map("2"));
 
-        mapper = create(map, "A");
+        mapper = create(map, false, "A");
+
+        assertNull(mapper.map(null));
+        assertEquals("101", mapper.map("1"));
+        assertEquals("2", mapper.map("2"));
+
+        mapper = create(map, true, "A");
 
         assertEquals("A", mapper.map(null));
-        assertEquals("A", mapper.map(""));
         assertEquals("101", mapper.map("1"));
         assertEquals("A", mapper.map("2"));
     }
@@ -106,17 +99,18 @@ public class BasicUserNameMapperSelfTest extends GridCommonAbstractTest
{
      * Create mapper.
      *
      * @param dictionary Dictionary.
+     * @param useDfltUsrName Whether to use default user name.
      * @param dfltUsrName Default user name.
      * @return Mapper.
      */
-    private BasicUserNameMapper create(@Nullable Map<String, String> dictionary, @Nullable
String dfltUsrName) {
+    private BasicUserNameMapper create(@Nullable Map<String, String> dictionary, boolean
useDfltUsrName,
+        @Nullable String dfltUsrName) {
         BasicUserNameMapper mapper = new BasicUserNameMapper();
 
         mapper.setMappings(dictionary);
+        mapper.setUseDefaultUserName(useDfltUsrName);
         mapper.setDefaultUserName(dfltUsrName);
 
-        mapper.start();
-
         return mapper;
     }
 }


Mime
View raw message