Changeset 2024

Show
Ignore:
Timestamp:
03/31/08 16:39:07 (8 months ago)
Author:
apatel
Message:

removed CPolicyContainer, it seemed unnecessary after refactoring its functionality to System.
Looks like refreshpolicy is being called more than it should have, don't know the solution.
Tried to remove resource listener by catching remove event and comparing it to the system folder.
Seems to work.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/custome_policy/framework-plugin/src/com/tresys/framework/plugin/builder/SELinuxSystem.java

    r2022 r2024  
    7474import com.tresys.slide.plugin.nature.SystemProjectNature; 
    7575import com.tresys.slide.plugin.nature.PolicyXMLContainer.PolicyXMLListener; 
    76 import com.tresys.slide.utility.policyxmlparser.BasePartCreator; 
     76import com.tresys.slide.utility.policyxmlparser.IPolicyPartCreator; 
    7777import com.tresys.slide.utility.policyxmlparser.MacroParseException; 
    7878import com.tresys.slide.utility.policyxmlparser.MacroParser; 
     
    8080public class SELinuxSystem  
    8181        extends PlatformObject  
    82         implements IWorkbenchAdapter, PolicyXMLListener, IAnalysisListener 
     82        implements IWorkbenchAdapter, PolicyXMLListener, IAnalysisListener, IResourceChangeListener 
    8383{ 
    8484 
     
    105105        /** Folder of the cds policy */ 
    106106        private IFolder mLayer; 
    107          
    108         /** Container that knows everythign about custom policy */ 
    109         private CPolicyContainer mCustomPolicy; 
    110107         
    111108        /** Name of the system */ 
     
    140137 
    141138                m_mlsSupport = i_mlsSupport; 
    142 //              mCustomPolicy = getCustomPoilcyContainer(); 
    143139                AnalysisContainer.getContainer ().addListener (m_folder, this); 
     140                ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 
    144141        } 
    145142         
     
    215212                                                try  
    216213                                                { 
    217                                                         getCustomPoilcyContainer ().refreshPolicy (); 
     214                                                        refreshPolicy (); 
    218215                                                         
    219216                                                        GraphicPolicyRefresh refresh = new GraphicPolicyRefresh (system); 
     
    535532        } 
    536533         
    537         public final IFolder getDefaultLayer() 
     534        private final String getCustomModuleName() 
     535        { 
     536                return SELinuxPolicy.CUSTOM_PREFIX + getName (); 
     537        } 
     538         
     539        /** 
     540         * Creates a layer if it does not exist. Must not be called 
     541         * from the workspace modification thread. 
     542         * @return 
     543         */ 
     544        public IFolder getDefaultLayer() 
    538545        { 
    539546                if (mLayer != null && mLayer.exists ()) 
    540547                        return mLayer; 
    541                 mLayer = m_folder.getFolder (CDS_LAYER); 
     548                 
     549                mLayer = m_folder.getFolder(CDS_LAYER); 
    542550                if (mLayer.exists ()) 
    543551                        return mLayer; 
    544  
    545552                try 
    546553                { 
     
    560567        } 
    561568         
    562         public CPolicyContainer getCustomPoilcyContainer() 
    563         { 
    564                 if( mCustomPolicy == null ) 
    565                         mCustomPolicy = new CPolicyContainer(getName ()); 
    566                 return mCustomPolicy; 
    567         } 
    568          
    569569        public void Translate() 
    570570                throws CoreException 
     
    582582 
    583583                final String policyPath = "cdsframework-" + m_name; 
    584  
     584                 
    585585                final IFile teFile = getDefaultLayer ().getFile(policyPath + '.' + BaseProjectNature.TE_EXTENSION); 
    586586                final IFile ifFile = getDefaultLayer ().getFile(policyPath + '.' + BaseProjectNature.IF_EXTENSION);              
     
    763763        } 
    764764         
    765         public class CPolicyContainer  
    766                 implements IResourceChangeListener 
    767         { 
    768                 public static final int IF_FILE = 0; 
    769                 public static final int TE_FILE = 1; 
    770                 public static final int FC_FILE = 2; 
    771                  
    772                 protected final String m_sModuleName; 
    773                  
    774                 /** Three module IFiles */ 
    775                 protected IFile[] mModule; 
    776  
    777                 public CPolicyContainer(String i_sModuleName) 
    778                 { 
    779                         m_sModuleName = SELinuxPolicy.CUSTOM_PREFIX + i_sModuleName; 
    780                         init(); 
    781                 } 
    782                  
    783                 void init() 
    784                 { 
    785                         IFolder layer = getDefaultLayer (); 
    786                         if (layer == null) 
    787                                 return; 
    788  
    789                         String if_file = m_sModuleName + '.' + BaseProjectNature.IF_EXTENSION; 
    790                         String te_file = m_sModuleName + '.' + BaseProjectNature.TE_EXTENSION; 
    791                         String fc_file = m_sModuleName + '.' + BaseProjectNature.FC_EXTENSION; 
    792                          
    793                         final IFile interface_file = layer.getFile (if_file); 
    794                         final IFile type_file = layer.getFile(te_file); 
    795                         final IFile file_context_file = layer.getFile (fc_file); 
    796                          
    797                         if( interface_file.exists () && 
    798                                 type_file.exists () && 
    799                                 file_context_file.exists () ) 
     765        public IFile[] getCustomModuleFiles() 
     766        { 
     767                IFolder layer = getDefaultLayer (); 
     768                 
     769                if (layer == null) 
     770                        return null; 
     771                 
     772                String module_name = SELinuxPolicy.CUSTOM_PREFIX + getName (); 
     773                 
     774                String if_filename = module_name + '.' + BaseProjectNature.IF_EXTENSION; 
     775                String te_filename = module_name + '.' + BaseProjectNature.TE_EXTENSION; 
     776                String fc_filename = module_name + '.' + BaseProjectNature.FC_EXTENSION; 
     777                 
     778                IFile if_file = layer.getFile (if_filename);  
     779                IFile te_file = layer.getFile(te_filename); 
     780                IFile fc_file = layer.getFile(fc_filename); 
     781                 
     782                if( if_file.exists () && te_file.exists () && fc_file.exists () ) 
     783                { 
     784                        return new IFile []{if_file, te_file, fc_file}; 
     785                } 
     786                 
     787                Object [][]array =  
     788                { 
     789                        {if_file,  
     790                                "## <summary>\n" + 
     791                                "## Customizations for CDS Framework generated policy for " + getName () + "\n" + 
     792                                "## </summary>\n" + 
     793                                "## <desc>\n" + 
     794                                "## <p>\n" + 
     795                                "## Please only use these customizations when no other means of \n" + 
     796                                "## customizing your policy is working.  Please use this as a \n" + 
     797                                "## last resort efort try to use custom dictionary entry, ability,\n" + 
     798                                "## base resource or base domain first.\n" + 
     799                                "## </p>\n" + 
     800                                "## </desc>\n" 
     801                        }, 
     802                        {te_file, "policy_module("+ module_name + "," + "1.0.0) \n\n"}, 
     803                        {fc_file, "\n"}, 
     804                }; 
     805                 
     806                IFile []module_files = new IFile[array.length]; 
     807                 
     808                try 
     809                { 
     810                        for( int i=0; i < module_files.length; i++) 
    800811                        { 
    801                                 ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 
    802                                 mModule = new IFile[3]; 
    803                                 mModule[IF_FILE] = interface_file; 
    804                                 mModule[TE_FILE] = type_file; 
    805                                 mModule[FC_FILE] = file_context_file; 
    806 //                              final File changed_file = interface_file.getRawLocation ().toFile (); 
     812                                IFile file = (IFile)array[i][0]; 
     813                                InputStream stream = new ByteArrayInputStream (((String)array[i][1]).getBytes()); 
     814                                module_files[i] = file; 
     815                                //TODO::arp::run some test. 
     816                                if( module_files[i].exists() ) 
     817                                { 
     818                                        module_files[i].appendContents (stream, IResource.KEEP_HISTORY, null); 
     819                                } 
     820                                else 
     821                                { 
     822                                        module_files[i].create (stream, true, null); 
     823                                } 
     824                        } 
     825                } 
     826                catch(CoreException ce) 
     827                { 
     828                        ce.printStackTrace (); 
     829                        return null; 
     830                } 
     831                return module_files; 
     832        } 
     833         
     834        public void resourceChanged (IResourceChangeEvent event) 
     835        { 
     836                IResourceDelta rootDelta = event.getDelta(); 
     837                 
     838                IPath path = getFolder ().getFullPath (); 
     839                IResourceDelta interested_root = rootDelta.findMember(path); 
     840                 
     841                final String if_filename = getCustomModuleName () + "." + BaseProjectNature.IF_EXTENSION; 
     842                 
     843                IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() 
     844                { 
     845                        public boolean visit(IResourceDelta delta) 
     846                        { 
     847                                //not interested in markers 
     848                                if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) 
     849                                { 
     850                                        return true; 
     851                                } 
     852 
     853                                if ((delta.getKind() & IResourceDelta.ADDED) == 0 && 
     854                                                (delta.getKind() & IResourceDelta.CHANGED) == 0 && 
     855                                                (delta.getKind() & IResourceDelta.REMOVED) == 0) 
     856                                { 
     857                                        return true; 
     858                                } 
    807859                                 
    808                                 refreshPolicy (); 
     860                                IResource resource = delta.getResource(); 
     861                                 
     862                                if( delta.getKind () == IResourceDelta.REMOVED && resource.getType () == IResource.FOLDER ) 
     863                                { 
     864                                        if( resource.equals (getFolder ()) ) 
     865                                        { 
     866                                                ResourcesPlugin.getWorkspace ().removeResourceChangeListener (SELinuxSystem.this); 
     867                                                return false; 
     868                                        } 
     869                                } 
     870                                 
     871                                if (resource.getType() == IResource.FILE  
     872                                        && resource.getName ().equals (if_filename)) 
     873                                { 
     874                                        refreshPolicy (); 
     875                                        return false; 
     876                                } 
     877                                return true; 
    809878                        } 
    810                 } 
    811                  
    812                 void dispose() 
    813                 { 
    814                         ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 
    815                         mModule = null; 
    816                 } 
    817                  
    818                 public void refreshPolicy () 
    819                 { 
    820                         Job parseFileJob = new Job("Parsing custom module files") 
     879                }; 
     880                try 
     881                { 
     882                        if( interested_root != null ) 
     883                                interested_root.accept (visitor); 
     884                } 
     885                catch (CoreException e) 
     886                { 
     887                        e.printStackTrace(); 
     888                } 
     889        } 
     890 
     891        public void refreshPolicy () 
     892        { 
     893                final String module_name = getCustomModuleName (); 
     894                Job parseFileJob = new Job("Parsing custom module files") 
     895                { 
     896                        public IStatus run(IProgressMonitor monitor) 
    821897                        { 
    822                                 public IStatus run(IProgressMonitor monitor) 
    823                                 { 
    824                                         try 
    825                                         { 
    826                                                 IProject proj = getProject (); 
    827                                                 if( proj == null || !proj.exists ()) 
    828                                                         return Status.CANCEL_STATUS; 
     898                                try 
     899                                { 
     900                                        IProject proj = getProject (); 
     901                                        if( proj == null || !proj.exists ()) 
     902                                                return Status.CANCEL_STATUS; 
     903                                         
     904                                        SLIDEProjectNature nat = SLIDEProjectNature.getNature(getProject ()); 
     905                                         
     906                                        PolicyXMLContainer policyXML = nat.getPolicyXML (getFolder ()); 
     907                                        if ( policyXML == null ) 
     908                                                return new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), "Can not load policy"); 
     909                                         
     910                                        IPolicyPartCreator part_creator = policyXML.getCreator (); 
     911                                        if( part_creator == null ) 
     912                                                return Status.CANCEL_STATUS; 
     913                                         
     914                                        String if_filename = module_name + '.' + BaseProjectNature.IF_EXTENSION; 
     915                                         
     916                                        IFile file = getFolder ().getFolder (CDS_LAYER).getFile (if_filename); 
     917                                        if( file == null || !file.exists () ) 
     918                                                return Status.CANCEL_STATUS; 
    829919                                                 
    830                                                 SLIDEProjectNature nat = SLIDEProjectNature.getNature(getProject ()); 
    831                                                  
    832                                                 PolicyXMLContainer policyXML = nat.getPolicyXML (getFolder ()); 
    833                                                 if ( policyXML == null ) 
    834                                                         return new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), "Can not load policy"); 
    835                                                  
    836                                                 if( mModule == null || mModule[IF_FILE] == null ) 
    837                                                         return Status.CANCEL_STATUS; 
    838                                                          
    839                                                 IPath interfacePath = mModule[IF_FILE].getLocation (); 
    840                                                 File interfaceFile = interfacePath.toFile (); 
    841                                                  
    842                                                 Map modified_templates = MacroParser.parseM4File (interfaceFile, MacroParser.TEMPLATE, policyXML.getCreator ()); 
    843                                                 CPolicyRefresher refresher = new CPolicyRefresher(modified_templates.keySet(), SELinuxPolicy.CUSTOM_PREFIX, SELinuxSystem.this.getName ()); 
    844                                                 getPolicy ().Accept(refresher); 
    845                                         } 
    846                                         catch (MacroParseException pe) 
    847                                         { 
    848                                                 IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), pe.getLocalizedMessage ()); 
    849                                                 return status; 
    850                                         } 
    851                                         catch (FileNotFoundException fne) 
    852                                         { 
    853                                                 IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), fne.getLocalizedMessage (), fne); 
    854                                                 return status; 
    855                                         } 
    856                                         return Status.OK_STATUS; 
    857                                 } 
    858                         }; 
    859                         parseFileJob.schedule(); 
    860                 } 
    861                  
    862                 public IFile[] getCustomModuleFiles() 
    863                 { 
    864                         IFolder layer = getDefaultLayer (); 
    865                         if (layer == null) 
    866                                 return null; 
    867                         if (mModule != null) 
    868                                 return mModule; 
    869                          
    870                         String if_file = m_sModuleName + '.' + BaseProjectNature.IF_EXTENSION; 
    871                         String te_file = m_sModuleName + '.' + BaseProjectNature.TE_EXTENSION; 
    872                         String fc_file = m_sModuleName + '.' + BaseProjectNature.FC_EXTENSION; 
    873                          
    874                         Object [][]array =  
    875                         { 
    876                                 {layer.getFile (if_file),  
    877                                         "## <summary>\n" + 
    878                                         "## Customizations for CDS Framework generated policy for " + getName () + "\n" + 
    879                                         "## </summary>\n" + 
    880                                         "## <desc>\n" + 
    881                                         "## <p>\n" + 
    882                                         "## Please only use these customizations when no other means of \n" + 
    883                                         "## customizing your policy is working.  Please use this as a \n" + 
    884                                         "## last resort efort try to use custom dictionary entry, ability,\n" + 
    885                                         "## base resource or base domain first.\n" + 
    886                                         "## </p>\n" + 
    887                                         "## </desc>\n" 
    888                                 }, 
    889                                 {layer.getFile (te_file), "policy_module("+ m_sModuleName + "," + "1.0.0) \n\n"}, 
    890                                 {layer.getFile (fc_file), "\n"}, 
    891                         }; 
    892                          
    893                         try 
    894                         { 
    895                                 mModule = new IFile[array.length]; 
    896                                 for( int i=0; i < mModule.length; i++) 
    897                                 { 
    898                                         IFile file = (IFile)array[i][0]; 
    899                                         InputStream stream = new ByteArrayInputStream (((String)array[i][1]).getBytes()); 
    900                                         mModule[i] = file; 
    901                                         //TODO::arp::run some test. 
    902                                         if( mModule[i].exists() ) 
    903                                         { 
    904                                                 mModule[i].appendContents (stream, IResource.KEEP_HISTORY, null); 
    905                                         } 
    906                                         else 
    907                                         { 
    908                                                 mModule[i].create (stream, true, null); 
    909                                         } 
    910                                 } 
     920                                        IPath interfacePath = file.getLocation (); 
     921                                        File interfaceFile = interfacePath.toFile (); 
     922                                         
     923                                        if( !interfaceFile.canRead () ) 
     924                                                return Status.CANCEL_STATUS; 
     925                                        Map modified_templates = MacroParser.parseM4File (interfaceFile, MacroParser.TEMPLATE, part_creator ); 
     926                                        CPolicyRefresher refresher = new CPolicyRefresher(modified_templates.keySet(), SELinuxPolicy.CUSTOM_PREFIX, SELinuxSystem.this.getName ()); 
     927                                        getPolicy ().Accept(refresher); 
     928                                } 
     929                                catch (MacroParseException pe) 
     930                                { 
     931                                        IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), pe.getLocalizedMessage ()); 
     932                                        return status; 
     933                                } 
     934                                catch (FileNotFoundException fne) 
     935                                { 
     936                                        IStatus status = new Status(IStatus.ERROR, SEFramework_Plugin.getUniqueIdentifier (), fne.getLocalizedMessage (), fne); 
     937                                        return status; 
     938                                } 
     939                                return Status.OK_STATUS; 
    911940                        } 
    912                         catch(CoreException ce) 
    913                         { 
    914                                 ce.printStackTrace (); 
    915                                 mModule = null; 
    916                                 return mModule; 
    917                         } 
    918                         ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); 
    919                         return mModule; 
    920                 } 
    921                  
    922                 public void resourceChanged (IResourceChangeEvent event) 
    923                 { 
    924                         IResourceDelta rootDelta = event.getDelta(); 
    925  
    926                         if (mModule == null) 
    927                                 return; 
    928                          
    929                         IPath path = mModule[0].getFullPath(); 
    930                         IResourceDelta interested_root = rootDelta.findMember(path); 
    931                          
    932                         IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() 
    933                         { 
    934                                 public boolean visit(IResourceDelta delta) 
    935                                 { 
    936                                         //not interested in markers 
    937                                         if ((delta.getFlags() & IResourceDelta.MARKERS) != 0) 
    938                                         { 
    939                                                 return true; 
    940                                         } 
    941  
    942                                         if ((delta.getKind() & IResourceDelta.ADDED) == 0 && 
    943                                                         (delta.getKind() & IResourceDelta.CHANGED) == 0 && 
    944                                                         (delta.getKind() & IResourceDelta.REMOVED) == 0) 
    945                                         { 
    946                                                 return true; 
    947                                         } 
    948                                          
    949                                         IResource resource = delta.getResource(); 
    950                                         if (resource.getType() == IResource.FILE  
    951                                                 && resource.getName ().equals (mModule[IF_FILE].getName ())) 
    952                                         { 
    953                                                 if( delta.getKind () == IResourceDelta.REMOVED ) 
    954                                                 { 
    955                                                         dispose (); 
    956                                                         return false; 
    957                                                 } 
    958                                                 refreshPolicy (); 
    959                                         } 
    960                                         return true; 
    961                                 } 
    962                         }; 
    963                         try 
    964                         { 
    965                                 if( interested_root != null) 
    966                                         interested_root.accept(visitor); 
    967                         } 
    968                         catch (CoreException e) 
    969                         { 
    970                                 e.printStackTrace(); 
    971                         } 
    972                 } 
     941                }; 
     942                parseFileJob.schedule(); 
    973943        } 
    974944} 
  • branches/custome_policy/framework-plugin/src/com/tresys/framework/plugin/editor/action/AddCustomPolicy.java

    r2017 r2024  
    3131import com.tresys.framework.plugin.SEFramework_Plugin; 
    3232import com.tresys.framework.plugin.builder.SELinuxSystem; 
    33 import com.tresys.framework.plugin.builder.SELinuxSystem.CPolicyContainer; 
    3433import com.tresys.framework.plugin.editor.policy.graphic.GraphicPolicyEditor; 
    3534import com.tresys.framework.plugin.editor.policy.graphic.GraphicPolicyMetaVisitor; 
     
    8180                try 
    8281                { 
    83                         CPolicyContainer container = mEditor.getSystem ().getCustomPoilcyContainer (); 
    84                         IFile []files = container.getCustomModuleFiles(); 
     82                        IFile []files = mEditor.getSystem ().getCustomModuleFiles (); 
    8583                         
    8684                        if( files == null ) 
     
    9088                        } 
    9189                                 
    92                         if( files[CPolicyContainer.IF_FILE].exists () ) 
     90                        if( files[ModuleEditorInput.IF_EDITOR_INDEX].exists () ) 
    9391                        { 
    9492                                /** 
     
    103101                                 
    104102                                ByteArrayInputStream input = new ByteArrayInputStream(stream.toString ().getBytes ()); 
    105                                 files[CPolicyContainer.IF_FILE].appendContents (input, IResource.KEEP_HISTORY, null); 
     103                                files[0].appendContents (input, IResource.KEEP_HISTORY, null); 
    106104                        } 
    107105                        /**