1/*
2 * Copyright (C) 2011 The Android Open Source Project
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
18package android.filterfw.core;
19
20import java.lang.reflect.Field;
21
22/**
23 * @hide
24 */
25public class ProgramPort extends FieldPort {
26
27    protected String mVarName;
28
29    public ProgramPort(Filter filter,
30                       String name,
31                       String varName,
32                       Field field,
33                       boolean hasDefault) {
34        super(filter, name, field, hasDefault);
35        mVarName = varName;
36    }
37
38    @Override
39    public String toString() {
40        return "Program " + super.toString();
41    }
42
43    @Override
44    public synchronized void transfer(FilterContext context) {
45        if (mValueWaiting) {
46            try {
47                Object fieldValue = mField.get(mFilter);
48                if (fieldValue != null) {
49                    Program program = (Program)fieldValue;
50                    program.setHostValue(mVarName, mValue);
51                    mValueWaiting = false;
52                }
53            } catch (IllegalAccessException e) {
54                throw new RuntimeException(
55                    "Access to program field '" + mField.getName() + "' was denied!");
56            } catch (ClassCastException e) {
57                throw new RuntimeException("Non Program field '" + mField.getName()
58                    + "' annotated with ProgramParameter!");
59            }
60        }
61    }
62}
63